3.10. Using the iostream Iterators
Imagine that we have been given the task of reading a sequence of string elements from standard input, storing them into a vector, sorting them, and then writing the words back to standard output. A typical solution looks like this:
#include <iostream> #incluse <string> #include <vector> #include <algorithm> using namespace std; int main() { string word; vector<string> text; // ok: let's read each word in turn until done while ( cin >> word ) text.push_back( word ); // ok: sort it sort( text.begin(), text.end() ); // ok: let's write them back for ( int ix = 0; ix < text.size(); ++ix ) cout << text[ ix ] << ' '; }
The standard library defines both input and output iostream iterator classes, called istream_iterator ...
Get Essential C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.