stringstream的使用 UVA 10815

水题题目描述就不写了

主要是发现stringstream真的是好用,可以把string绑定到stringstream中,然后就能以空格为分隔符分割出每个单词,听说每次重新创建stringstream开销是巨大的,但是这题两种写法时间上并无太大差别

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <sstream>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <set>
 8 #include <cmath>
 9 #pragma warning ( disable : 4996 )
10 
11 using namespace std;
12 
13 int Max( int a, int b ) { return a>b?a:b; }
14 int Min( int a, int b ) { return a>b?b:a; }
15 
16 const int inf = 0x3f3f3f3f;
17 const int maxn = 1e5+5;
18 
19 set<string> str;
20 
21 
22 int main()
23 {
24     string s, buf;
25     //stringstream ss;
26     while ( getline(cin, s) ) 
27     {
28         //ss.clear();
29         for( int i = 0; i < s.length(); i++ )
30             s[i] = isalpha(s[i])?tolower(s[i]):' ';
31 
32         //ss << s;
33         stringstream ss(s);
34         while ( ss >> buf )        //每次从stringstream中读取一个单词
35             str.insert(buf);
36     }
37 
38     for ( set<string>::iterator it = str.begin(); it != str.end(); it++ )
39         cout << *it << endl;
40     
41     return 0;
42 }

 

posted @ 2018-03-08 19:37  LBNOQYX  阅读(120)  评论(0编辑  收藏  举报