博客版权已保护:转载请注明出处,谢谢。

Andy's First Dictionary (set)

博客主要参照:https://www.cnblogs.com/yjlblog/p/6947747.html ,感谢前辈们分享的经验!

1.学会运用set容器;(有个小知识点,set容器,元素只能出现一次,并且插入可以从小到大排序)

2.学习字符函数库中常用的函数

3.学会stringstream(可参考这篇博文:http://blog.csdn.net/xw20084898/article/details/21939811)

4.最后运行记得是  在空行  ctrl+z +回车。(至于为什么,参考博文:http://blog.csdn.net/kevin_ut/article/details/8576740)

说明:

set是一个集合,集合中的元素不重复 且集合中的元素是有序的(自动有序化)

set不是数组 只能通过迭代器(iterator)把里面的元素倒出来 迭代器相当于是指针 扫描的是地址 因此输出的时候需要用*variation

#include<iostream>
#include<set>
#include<sstream>
#include<string>
using namespace std;
int main()
{
    set <string> dict;
    string a,buf;
    while(cin >> a)
    {
        for(int i=0;i<a.size();i++)
        {
            if(isalpha(a[i])) a[i] = tolower(a[i]);
            else a[i] = ' ';
        }
        stringstream aa(a);
        while(aa >> buf) dict.insert(buf);
    }
    set <string> :: iterator it;
    for(it = dict.begin();it != dict.end();it++)
        cout << *it << endl;
    return 0;
}

isalpha()判断是否是字母 如果是就用头tolower()转换成小写 否则就转为空格 这样是为了字符串流重读入的时候能够把空格去掉

 

补充:

重点:学习sstream库还有string库的博客:https://blog.csdn.net/xw20084898/article/details/21939811 (stringstream)

再次补充个例子,用stringstream转化的数据流,是会忽略掉空格的。

 

#include<sstream>
#include<iostream>
using namespace std;
int main()
{
    stringstream aa;
    string str1 = "   38     ";
    string str2 = "   he llo     ";
    int num;
    string buf;
    aa << str1;
    aa >> buf;
    cout << buf <<endl;
    aa.clear();
    aa << str2;
    aa >> buf;
    cout << buf << endl;
    return 0;
}

stringstream 转化的时候,会以空格作为结束的判断,所以输出的结果为:

38

he

另外,stringstream支持各种格式之间的转换,学习博客:https://blog.csdn.net/xw20084898/article/details/21939811 (stringstream)

posted on 2018-07-31 21:05  superhero11  阅读(227)  评论(0编辑  收藏  举报

导航