gpt4 book ai didi

c++ - 将字符串拆分为单词 vector

转载 作者:行者123 更新时间:2023-11-27 23:03:24 25 4
gpt4 key购买 nike

<分区>

我有一个 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,正确的代码是什么?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com