#include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
#include<set>//因为可能有重复的单词,而set可以去重,使用起来更好。
using namespace std;
string str,str2;
//stringstream ss;
set<string> jh;
int main() {
while (cin >> str && str!="haha") {
for (int i = 0; i < str.length(); i++) {//去除非单词字符
if (isalpha(str[i])) {
str[i] = tolower(str[i]);
}
else {
str[i] = ' ';
}
}
//ss << str; ???为啥不能这样写?
stringstream ss(str);
while (ss >> str)
jh.insert(str);
}
for (set<string>::iterator it = jh.begin(); it != jh.end(); it++) {
cout << *it << endl;
}
return 0;
}
试题地址:https://www.luogu.com.cn/problem/UVA10815
因为你定义了ss后没有清空,导致运行结果不对。试一下在使用ss前添加
ss.str("");
你可以这么做 stringstream ss;ss<<str;