UVA 10815 Andy's First Dictionary【set】

题目链接:https://vjudge.net/contest/211547#problem/C

题目大意:

输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出,单词不区分大小写。

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

……

#include<iostream>
#include<string>
#include<set>
#include<sstream>
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] = ' ';          //一个一连串的字符串,如果带有其它符号,就会被分割成几个字符串
    stringstream ss(s);             //形成可能带有空格的字符串流
    while(ss >> buf) dict.insert(buf);         //将字符串流中的单词一个一个的存储在集合内
  }
  for(set<string>::iterator it = dict.begin(); it != dict.end(); ++it)                //set具有给集合内元素去重和自动排序的功能
    cout << *it << "
";
  return 0;
}

2018-04-05


作者:is_ok
出处:http://www.cnblogs.com/00isok/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/00isok/p/8722099.html