i have this code i found to cut a string into words.
i cant figure out how the while part works. how does it know to extract words without whitespaces to the buf variable?
it seems the extraction operator (>>) is used both to progress bits into the buffer, and to return true for the loop – i just cant figure out how it knows to cut the words by whitespaces.
string buf; // Have a buffer string
stringstream ss(str); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
That is
std::operator>>, not the bitwise operator, and is used to extract formatted data, in this case astd::string. It returns a reference to the stream being read.A
stringstreamcan be used in a boolean context due to its conversionoperator void*(), allowing it to be used as the terminating condition in the loop.