stringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,它不会将空格作为流。这样就实现了字符串的空格切割。
#include<bits/stdc++.h>
#include<sstream> //头文件
using namespace std;
int main(){
string str="nice to meet you";
stringstream stream(str);
string s;
while(stream>>s){
cout<<s<<endl;
}
return 0;
}
输出:
nice
to
meet
you
#include<bits/stdc++.h>
using namespace std;
int main() {
string str1,str2;
while(getline(cin,str1)) {
if(str1 == "#")
break;
stringstream stream(str1);
set<string> tp;
while(stream>>str2) {
tp.insert(str2);
}
cout<<tp.size()<<endl;
}
return 0;
}