uva10815 by sixleaves

题目很简单。其实stringstream就的用法和iosteam差不多,所以学习起来是很简单的。stringstream类里面有一个string缓存,str()和str(string)成员函数。
前者用于把stringstream的返回string缓存,后者是把string对象包装成stringstream对象。stringstream类存在于sstream头文件中。其是独立于标准I/O但有类似
功能的类。clear主要是为了多次使用stringstream,因为stringstream每次的构造都十分耗时,所以最后能多次,反复使用,就如同面向对象里面的单例模式。
代码如下:
 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <sstream>
 5 
 6 
 7 using namespace std;
 8 
 9 set<string> dict;
10 stringstream ss;
11 
12 /*
13  主要学习两个重点stringstream的用法与set容器的insert、迭代器的用法。
14  */
15 int main() {
16 
17     string s,buf;
18     
19     while (cin >> s) {
20         int len = s.size();
21         for (int i = 0; i < len; i++) {
22             if (isalpha(s[i])) s[i] = tolower(s[i]); else s[i] = ' ';
23         }
24         ss.clear();
25         ss.str(s);
26         while (ss >> buf) dict.insert(buf);
27     }
28     
29     for (set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
30         cout << *it << endl;
31     return 0;
32 }
posted @ 2015-06-05 00:29  sixleaves  阅读(133)  评论(0编辑  收藏  举报