题目链接:Andy's First Dictionary - UVA 10815 - Virtual Judge (vjudge.net)
题目分析:多实例输入字符串,然后将每个单词都变小写,最后排序去重输出每个单词。
解题思路:set是一个是一个内部有序且不含重复元素的容器,可以直接将每个单词存入set中,最后遍历输出。
实现代码:
#include <iostream>
#include <set>
#include <string>
#include <sstream>
using namespace std;
int main() {
string s;
set<string> st;
while(cin >> s) {
string tp;
for(int i=0;i<s.size();i++) {
if(isalpha(s[i])) s[i] = tolower(s[i]); //变小写
else s[i]=' ';
}
stringstream ss(s); //每个字符串可能包含多个单词
while(ss >> tp) st.insert(tp);
}
for(auto it=st.begin();it!=st.end();it++) {
cout << *it << endl;
}
}