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;
}