Loading

string以空格分割字符串

istringstream 分割

#include<sstream> //istream

string text = "aaa bb ccc d";
istringstream is(text);
string word;
while (is >> word) {
    cout<<word<<endl;
}

输出结果:

aaa
bb
ccc
d

还可以直接调库:

istringstream iss{text};
vector<string> words(istream_iterator<string>(iss),{});
for (auto w:words) { 
    cout<<w<<endl;
}
posted @ 2021-12-26 21:39  柴承训  阅读(446)  评论(0编辑  收藏  举报