istream has the >> operator, but it skips new lines like it skips whitespace. How can I get a list of all the words in 1 line only, into a vector (or anything else that's convenient to use)?
Easiest way to get words of one line from istream into a vector?
938 views Asked by Oliver Zheng At
3
There are 3 answers
0
On
I would suggest using getline to buffer the line into a string, then using a stringstream to parse the contents of that string. For example:
string line;
getline(fileStream, line);
istringstream converter(line);
for (string token; converter >> token; )
vector.push_back(token);
Be wary of using C string reading functions in C++. The std::string I/O functions are much safer.
One possibility (though considerably more verbose than I'd like) is: