首页 > 代码库 > UVa10815 Andy's First Dictionary (STL)

UVa10815 Andy's First Dictionary (STL)

链接:http://acm.hust.edu.cn/vjudge/problem/18649
分析:set容器应用。set中每个元素最多只出现一次。自定义类型也可以构造set,必须定义“小于”运算符。set中的元素从小到大已排好序。

 1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 #include <set> 5 #include <sstream> 6 using namespace std; 7  8 set<string> dict; 9 10 int main() {11     string s, buf;12     while (cin >> s) {13         for (int i = 0; i < s.length(); i++)14             if (isalpha(s[i])) s[i] = tolower(s[i]); else s[i] =  ;15         stringstream ss(s);16         while (ss >> buf) dict.insert(buf);17     }18     for (set<string>:: iterator it = dict.begin(); it != dict.end(); it++)19         cout << *it << endl;20     return 0;21 }

 

UVa10815 Andy's First Dictionary (STL)