Read keyboard input to a vector : 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 <string>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;

int main()
{
    /* create a string vector
     * - initialized by all words from standard input
     */
    vector<string> coll((istream_iterator<string>(cin)),
                        istream_iterator<string>());

    // sort elements
    sort (coll.begin(), coll.end());

    // print all elements ignoring subsequent duplicates
    unique_copy (coll.begin(), coll.end(),
                 ostream_iterator<string>(cout, "\n"));
}
Terminate batch job (Y/N)? n








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