Friday 27 December 2019

c++ - Split a string into a vector of words

I have a string like "ABC DEF " with whitespace at the end. I would like to convert it into a vector of strings like {"ABC" "DEF"}, so I used a stringstream:



string s = "ABC DEF ";
stringstream ss(s);

string tmpstr;
vector vpos;
while (ss.good())
{
ss >> tmpstr;
vpos.push_back(tmpstr);
}


However, the result vpos is {"ABC" "DEF" "DEF"}. Why the last word will be duplicated in the vector? And what is the correct code if using stringstream is required?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...