<分区>
我有一个 string
,例如 "ABC DEF "
,末尾有空格。我想将它转换成字符串的 vector
,例如 {"ABC""DEF"}
,所以我使用了 stringstream
:
string s = "ABC DEF ";
stringstream ss(s);
string tmpstr;
vector<string> vpos;
while (ss.good())
{
ss >> tmpstr;
vpos.push_back(tmpstr);
}
然而,结果 vpos
是 {"ABC""DEF""DEF"}
。为什么最后一个词会在 vector 中重复?如果需要使用 stringstream
,正确的代码是什么?