1,首先假设
string str = "i am a student";
字符串str必须是使用空格分开的。为了得到各个字串,可以进行按照空格的坐标进行分割,但是可以借助流进行自动分割,如下所示。
istringstream input(str);
string temp;
while(input >> temp){
cout<<temp<<endl;
}
/*
output:i
am
a
student
*/
应用:给你一个含有数字的字符串,求所有的数字。
string str = "1 9 10 188 9 122";
istringstream input(str);
string temp;
while(input >> temp){
cout<<stoi(temp)<<endl;
}