题意:给出一串单词,把所有单词改小写去重按字典序输出。
以前做过c的版本,现在用stl又写了一遍,当做练习。
set可以解决去重和排序问题。
代码:
#include <cstdio>
#include <iostream>
#include <string>
#include <cctype>
#include <set>
using namespace std;
set<string> d;
int main() {
string s;
char ch;
while (getline(cin, s)) {
for (int i = 0; i < s.size(); i++) {
if (!isalpha(s[i]))
continue;
string tmp;
while (i < s.size() && isalpha(s[i]))
tmp += tolower(s[i++]);
d.insert(tmp);
}
}
for (set<string>::iterator i = d.begin(); i != d.end(); i++)
cout << *i << endl;
return 0;
}