1.以空格分割
#include <iostream>
#include <sstream> //stringstream
using namespace std;
int main(){
string words("hello world thank you");
stringstream ss(words);
string temp;
while(ss >> temp){
cout << temp << "";
}
cout << "\n";
system("pause");
return 0;
}
运行结果
2.以符号分割
#include <iostream>
#include <sstream> //stringstream
using namespace std;
int main(){
string words("hello,world,thank,you");
stringstream ss(words);
string temp;
while(getline(ss, temp, ',')){ //以逗号, 作为分割符
cout << temp << "\n";
}
cout << "\n";
system("pause");
return 0;
}