Assign value to the last element : vector « vector « C++ Tutorial






/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
    // create empty vector for strings
    vector<string> sentence;

    // reserve memory for five elements to avoid reallocation
    sentence.reserve(5);

    // append some elements
    sentence.push_back("Hello,");
    sentence.push_back("how");
    sentence.push_back("are");
    sentence.push_back("you");
    sentence.push_back("?");

    // assign "!" to the last element
    sentence.back() = "!";

    // print elements separated with spaces
    copy (sentence.begin(), sentence.end(),
          ostream_iterator<string>(cout," "));
    cout << endl;
}
Hello, how are you !








16.1.vector
16.1.1.Demonstrating the simplest STL vector constructors: duplicate chars
16.1.2.Demonstrating the simplest STL vector constructors: empty vector
16.1.3.Use generic vector to create vector of chars
16.1.4.Use generic vector to create vector of integers
16.1.5.creates a vector of ints with zero elements
16.1.6.creates a vector of 10 ints with value 100
16.1.7.Use generic vector to create vector of strings
16.1.8.The Instantiation of vector
16.1.9.Using the vector
16.1.10.get an iterator that points to the maximum with max_element
16.1.11.Assign elements in int array to vector
16.1.12.Store a class object in a vector
16.1.13.Read keyboard input to a vector
16.1.14.Add class to a vector and then delete them one by one
16.1.15.Pass vector to a function
16.1.16.Computing an inner product of tuples represented as vectors
16.1.17.Assign value to the last element
16.1.18.Demonstrating STL vector copying constructors
16.1.19.Instantiate one vector and initialize it to the contents of another
16.1.20.Demonstrating STL vector constructors with a user-defined type and showing copying explicitly
16.1.21.Vector Init Array
16.1.22.Demonstrating STL vector constructors with a user-defined type