stl/ioiter1.cpp

The following code example is taken from the book
Object-Oriented Programming in C++
by Nicolai M. Josuttis, Wiley, 2002
© Copyright Nicolai M. Josuttis 2002


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>

int main()
{
    using namespace std;    // all symbols in std are global

    vector<string> coll;    // vector container for strings

    /* read strings from the standard input up until the end of the data
     * - copy from the `input collection' cin, inserting into coll
     */
    copy(istream_iterator<string>(cin),    // start of source range
         istream_iterator<string>(),       // end of source range
         back_inserter(coll));             // destination range

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

    /* output all elements
     * - copy from coll to the `output collection' cout
     * - every string on its own line (separated by "\n")
     */
    copy(coll.begin(), coll.end(),              // source range
         ostream_iterator<string>(cout,"\n"));  // destination range
}