C++ examples for STL:istream_iterator
Printing a Range to a Stream
#include <iostream> #include <string> #include <algorithm> #include <iterator> #include <vector> using namespace std; template<typename C> void printContainer(const C& c, char delim = ',', ostream& out = cout) { printRange(c.begin(), c.end(), delim, out); } template<typename Fwd> void printRange(Fwd first, Fwd last, char delim = ',', ostream& out = cout) { out << "{"; while (first != last) { out << *first;//w ww . j a v a 2 s . c o m if (++first != last) out << delim << ' '; } out << "}" << endl; } int main() { cout << "Enter a series of strings: "; istream_iterator<string> start(cin); istream_iterator<string> end; vector<string> v(start, end); copy(v.begin(), v.end(), ostream_iterator<string>(cout, ", ")); printContainer(v); printRange(v.begin(), v.end(), ';', cout); }