【发布时间】:2014-10-21 18:45:18
【问题描述】:
我有一个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"}。为什么最后一个词会在向量中重复?如果需要使用stringstream,正确的代码是什么?
【问题讨论】:
-
@CaptainObvlious:不是真的,不。
-
基本逻辑:
ss.good()无法预测未来! -
@LightnessRacesinOrbit - 非常正确。关闭投票被撤回。