I ran across a cool STL example that uses istream_iterators to copy from std input (cin) to a vector.
vector<string> col1;
copy(istream_iterator<string>(cin), istream_iterator<string>(),
back_inserter(col));
How would I do something similar to read from a file-stream directly into a container? Let’s just say its a simple file with contents:
“The quick brown fox jumped over the lazy dogs.”
I want each word to be a separate element in the vector after the copy line.
Replace
cinwith file stream object after opening the file successfully:In fact, you can replace
cinwith any C++ standard input stream.Got the idea?
colwill contain words of the string which you passed tostd::stringstream.