f(sixleaves) = sixleaves

重剑无锋 大巧不工

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  95 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks
题目很简单。其实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 on 2015-03-16 18:28 swp 阅读(240) 评论(0)  编辑 收藏 引用 所属分类: algorithm

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理