紫书 set安迪的第一个字典(Andy‘s First Dictionary,Uva 10815)代码及讲解

输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出。单
词不区分大小写。
样例输入:
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.
样例输出(为了节约篇幅只保留前 5 行):
a
adventures
blondes
came
disneyland
 
 
 
#include<iostream>
#include<string>
#include<set>
#include<sstream>
//得到字符串里的每个单词  
using namespace std;
set<string>dict;
main()
{ string s,buf;
  while(cin>>s)
  { for(int i=0;i<s.length();i++)
     if(isalpha(s[i]))
//判断字符是否为英文字母(小写字母返回2,大写为1)
       s[i]=tolower(s[i]); //把字母字符转换成小写,非字母不做处理
      else s[i]=' ';
      stringstream ss(s);
//将s复制到stringstream.ss里面 (ss随意取名字)  
      while(ss>>buf)  
//把ss里面的单词一个个输出 
       dict.insert(buf); // 将单词插入里面 
      
  }
  for(set<string>::iterator it=dict.begin();it!=dict.end();++it)
//集合排序  
    cout<<*it<<"\n"
//输出必须带上* 
    return 0;
 
 
记事本

set,集合,每个元素最多出现一次
s.insert (x) 把元素插入set集合里面 
set<int>::iterator it;  //集合排序
    for(it=s.begin ();it!=s.end ();it++)
    {
        printf("%d\n",*it); 
    }
//s.end()没有值
     cout<<"s.begain()   "<<*s.begin ()<<endl;
    //lower_bound()--返回指向大于(或等于)某值的第一个元素的迭代器
    cout<<"lower_buond  3  "<<*s.lower_bound (3)<<endl;
    
    //upper_bound()--返回大于某个值元素的迭代器
    cout<<"upper_bound  3  "<<*s.upper_bound (3)<<endl;

    //find()--返回一个指向被查找到元素的迭代器
    cout<<"find()  3   "<<*s.find (3)<<endl;

    cout<<"s.size()  "<<s.size ()<<endl;
   isalpha(a[i]) //判断字符是否为英文字母(小写字母返回2,大写为1)
   tolower(a[i]) //把字母字符转换成小写,非字母不做处理
 

 
 
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值