uva 10815,andy's first ditionary(set,stringstream的简单运用)

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

样例输入:

Adventures in DisneylandTwo blondes were going to Disneyland when they came to a fork in theroad. The sign read: "Disneyland Left."So they went home.

 

样例输出:

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

代码如下:

#include<set>
#include<iostream>
#include<sstream>
#include<string>
#include<cstring>
using namespace std;
 int main(int argc, char const *argv[]) {
  string s,temp;
  set<string>dict;
  set<string>::iterator it;
  stringstream ss;
  while(cin>>s){
    for(int i=0;i<s.length();i++){
      if (isalpha(s[i])) {
        s[i]=tolower(s[i]);//字母变为小写
      }
      else s[i]=' ';//将所有标点符号变为空格
      
      
    }stringstream ss(s);
      while(ss>>temp){
        dict.insert(temp);//将转换后的单词输入set
      }
  }
  for(it=dict.begin();it!=dict.end();it++){
    std::cout << *it << '\n';//按字典序输出
  }
  return 0;
}

 



上一篇:北京信息科技大学第十一届程序设计竞赛(重现赛)I


下一篇:Stomp