STL-set-stringstream分割字符-uva10815

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

Sample Input
Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."

So they went home.

Sample Output
a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<set>
#include<vector>
#include<sstream>
#define PI 3.1415926535897932384626
const int maxn=10000;
using namespace std;
set<string> dict;
string s, buf;
int main() {
  while(cin >> s) {
    for(int i = 0; i < s.length(); i++)
    if(isalpha(s[i])) s[i] = tolower(s[i]); else s[i] = ' ';//isalpha判断是否是字母,tolower将大写字母转为小写
    //cout<<s<<endl;
    stringstream ss(s);
    while(ss >> buf) //将s以空格分割单词,传到buf。
    dict.insert(buf);
  }
  for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
    cout << *it << endl;
  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41568836/article/details/81631364