Problem C Andy's First Dictionary(set的使用)

题目链接:Problem C

题意:输入一个文本,找出所有不同的单词,按照字典序从小到大输出,单词不区分大小写。

思路:将字母序列都存为小写,非字母的字符变成空格,然后利用stringstream实现sting对象的自动格式化。

note: 
stringstream对象的使用 #include
<sstream> #include<iostream> using namespace std; int main() { string line,word; while(getline(cin,line)) { stringstream stream(line); cout<<stream.str()<<endl; while(stream>>word){cout<<word<<endl;} } return 0; } /* * input: shanghai no1 school 1989 * output: shanghi no1 school 1989 * shanghai * no1 * school * 1989 */

code:

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

 

posted @ 2015-06-30 15:47  jasaiq  阅读(226)  评论(0编辑  收藏  举报