强烈安利
#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;
set<string> dict; //set会自动按排序好
int main(int argc, char** argv) {
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
string buf,s;
while (cin >> s){//如果要读取一行就使用getline(这一行默认是很长的,无论空格任何字符皆可);
// cout << s << endl;
for (int i = 0; i < s.length(); i++){
if(isalpha(s[i])){
s[i] = tolower(s[i]);
}
else s[i] = ' ';
}
stringstream ss(s); //一般stringstream用来去空格
while (ss >> buf) { //防止中间断开 比如先来个空格 后面就放弃了 所以一定要循环
// cout<<buf<<endl;
dict.insert(buf);
}
}
for(set<string>::iterator it = dict.begin(); it != dict.end(); it++){
cout << *it << endl;
}
return 0;
}
此处还要注意,就是isalpha和tolower的字符函数的使用,简化操作