K - Andy's First Dictionary(set可对多个字符串去重并按字典序排序)

Description

XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英语。于是他每天都读一篇只包含生词的英语文章,并以自己高达450的智商在一秒钟之内记忆下来。

现在给你一篇XY学长今天要读的文章,请你写一个程序,输出他都学习到了哪些单词。
要求:如果文章中有相同的单词,那么仅仅输出一次;而且如果两个单词只有大小写不同,将他们视为相同的单词。

Input

测试数据将输入一篇文章。不超过5000行,每一行最多200个字符,并以EOF结束。

Output

按照字典序输出他学到的单词,每行输出一个单词,输出单词时所有的字母全部小写。 
数据保证最多有5000个需要输出的单词。

Sample Input

样例输入①

a a a a a a a a, a a a a a a.
a a a a b a a a. a? a!!!

样例输入②

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
b

样例输出②

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

Hint

输入可能包含标点符号,但标点符号显然不能算作单词的一部分。

这个题就是c++模板的应用,只要把每个字符串逐个字符地判断(不能是标点符号)后放到set中利用set自动去重排序后输出就行了。

下面是几个模板的介绍:

头文件:
#include <cstdio>
#include <iostream>
#include <string>
#include <set>
#include <sstream>
#include <ctype.h>

<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。

  1.stringstream::str(); returns a string object with a copy of the current contents of the stream.

  2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.

  3.stringstream清空,stringstream s; s.str("");

  4.实现任意类型的转换

    template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
      stringstream stream;
      stream<<t;//向流中传值
      out_type result;//这里存储转换结果
      stream>>result;//向result中写入值
      return result;
        }

判断小写字母函数:isalpha()是bool型的函数,参数为一个字符,若该字符是小写字母则返回true,否则返回false
转化成小写字母函数:tolower()是char型参数,参数为一个字符,返回该字符的小写形式

完整代码:

#include <cstdio>
#include <iostream>
#include <string>
#include <set>
#include <sstream>
#include <ctype.h>
using namespace std;
string s,f;
set<string>st;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    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);//以下三行就是复制串s重新插入到set中(这里会忽略空格,所以标点就去掉了)
        while(ss>>f)
        st.insert(f);
    }
    set<string>::iterator it;
    for(it=st.begin();it!=st.end();it++)
        cout<<(*it)<<endl;
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值