UVA10815-5.3-Andy's First Dictionary(集合set的用法)

UVA10815-5.3-Andy’s First Dictionary(集合set的用法)
题目描述:
链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=835&problem=1756&mosmsg=Submission+received+with+ID+18662803
这个题的意思是给一段英文文章,找出文章中所有不相同的单词,按字典序的顺序输出出来。
题目分析:
这个题主要是学习set的用法,set也是c++里面的一个新的东西。
集合:set
set是数学上的一个集合,每个元素最多只出现一次。
而且书上说set已经定义了小于运算符,我的理解是输入的东西会自动从大到小排序。
这个题有理解一个新东西,steringstream ss,好像是用来把一些数据定义成一个流?然后向一个东西里面输入。
大概类似于这样用:

stringstream ss(s);
        while(ss>>buf)
                dict.insert(buf);

因为前面已经把s里面的非字母内容全部变成了空格,所以buf会一个单词一个单词的读入,不读空格。
这里有一个insert()函数。
我找到了一篇博文讲这个函数。
http://blog.csdn.net/hzw05103020/article/details/51785727
另外还有两行代码:

for(set<string>::iterator it = dict.begin();it != dict.end();++it)
        cout << *it << endl;

按书中解释,set::iterator 的意思是迭代器,是STL中的重要概念,类似于指针的用法。但我也不是很懂=。=

给出代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>

using namespace std;

set<string>dict;

int main()
{
    string s,buf;
    while(cin>>s)
    {
        for(int i=0;i<s.length();i++)
        {
            if(isalpha(s[i]))
                s[i]=tolower(s[i]);//tolower将大写字母变为小写
            else
                s[i]=' ';
        }
        stringstream ss(s);
        //ss<<s;
        while(ss>>buf)
                dict.insert(buf);
    }
    for(set<string>::iterator it = dict.begin();it != dict.end();++it)
        cout << *it << endl;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值