• 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)

  • 相关阅读:
    Python设置桌面壁纸
    youtube-dl使用介绍
    Matlab pcg函数的句柄形式之参数传递
    Sublime 安装支持GBK的插件
    MarkdownPad安装
    ug7.5经常卡死的解决方法
    HM NIS edit打包软件
    UG工程制图
    egg框架中是如何使用MD5加密的 实现用户修改密码 小编在这里献丑了。。。。。
    今天遇到了vue项目中使用Iconfont图标 ,感觉挺实用的 随手记录下。只需简单的6步就可以实现
  • 原文地址:https://www.cnblogs.com/myxdashuaige/p/9398269.html
Copyright © 2020-2023  润新知