C++STL1--set

一、说明

set的用法:单一元素,自动排序
set的方法:用编译器的提示功能即可,不需要自己记

二、简单测试

  1. /*
  2. 安迪的第一个字典
  3. set的用法:单一元素,自动排序
  4. set的方法:用编译器的提示功能即可,不需要自己记
  5. */
  6.  
  7. #include <iostream>
  8. #include <set>
  9. using namespace std;
  10. set<int> setTest;
  11.  
  12. int main(){
  13. int a[]={,,,,,,,,,};
  14. //将数组a里面的元素存入集合
  15. for(int i=;i<;i++)
  16. {
  17. setTest.insert(a[i]);//添加数据
  18. }
  19. //遍历输出,使用迭代器iterator
  20. //无序方式输入,输出时已经从小到大排好序了,并且重复元素没有输出
  21. for(set<int>::iterator it=setTest.begin();it!=setTest.end();++it)
  22. {
  23. cout<<*it<<" ";
  24. }
  25. cout<<endl;
  26. return ;
  27. }

结果说明:重复元素只存储一次,像3和8

并且会默认从小到大排序

三、实例

安迪的第一个字典
题目:
输入一个文本,找出所有不同的单词(连续的最序列),按字典序从小到大输出,单词不区分大小写。
样例输入:
Today was my friend’s birthday, and I was invited to her birthday party.
The party was so great and the theme was red color. I saw many pictures of Hello Kitty.
They were so lovely. We sang the song to her and played a lot of funny games.
At last, we sent our best wishes to her. We had a great time.
The sign read:"Disneyland Left."
样例输出:(前五个)
a
and
at
best
birthday

  1. /*
  2. 安迪的第一个字典
  3. 题目:
  4. 输入一个文本,找出所有不同的单词(连续的最序列),按字典序从小到大输出,单词不区分大小写。
  5. 样例输入:
  6. Today was my friend’s birthday, and I was invited to her birthday party.
  7. The party was so great and the theme was red color. I saw many pictures of Hello Kitty.
  8. They were so lovely. We sang the song to her and played a lot of funny games.
  9. At last, we sent our best wishes to her. We had a great time.
  10. The sign read:"Disneyland Left."
  11. 样例输出:(前五个)
  12. a
  13. and
  14. at
  15. best
  16. birthday
  17.  
  18. */
  19. /*
  20. set的用法:单一元素,自动排序
  21. set的方法:用编译器的提示功能即可,不需要自己记
  22. */
  23. /*
  24. 分析:
  25. 1、大写变小写,其它符号变空格
  26. 2、存入set
  27. */
  28. #include <iostream>
  29. #include <set>
  30. #include <string>
  31. #include <sstream>
  32. using namespace std;
  33. set<string> dict;//string集合
  34.  
  35. int main(){
  36. freopen("in.txt","r",stdin);
  37. string s,buf;//s用于读入单词,buf做缓存,解决read:"Disneyland是一个单词的问题
  38. while(cin>>s){
  39. for(int i=;i<s.length();i++){
  40. if(isalpha(s[i])) //判断是否是字符
  41. s[i]=tolower(s[i]);//是字符就变成小写
  42. else //不是字符就变成空格
  43. s[i]=' ';
  44. }
  45. stringstream ss(s);//字符串流,作用和inputstream很相似
  46. //引入stringstream是为了防止把read:"Disneyland 这样的形式看出一个单词
  47. while(ss>>buf) dict.insert(buf);//插入到dict集合中
  48. }
  49. //迭代器遍历,注意迭代器是指针
  50. for(set<string>::iterator it=dict.begin();it!=dict.end();it++)
  51. {
  52. cout<<*it<<endl;
  53. }
  54.  
  55. return ;
  56. }

四、常用用法

set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;另外,还得保证根节点左子树的高度与右子树高度相等。
平衡二叉检索树使用中序遍历算法,检索效率高于vector、deque和list等容器,另外使用中序遍历可将键值按照从小到大遍历出来。
构造set集合主要目的是为了快速检索,不可直接去修改键值。

常用操作:
1.元素插入:insert()
2.中序遍历:类似vector遍历(用迭代器)
3.反向遍历:利用反向迭代器reverse_iterator。
    例:
    set<int> s;
    ......
    set<int>::reverse_iterator rit;
    for(rit=s.rbegin();rit!=s.rend();rit++)
4.元素删除:与插入一样,可以高效的删除,并自动调整使红黑树平衡。
            set<int> s;
            s.erase(2);        //删除键值为2的元素
            s.clear();
5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。
            set<int> s;
            set<int>::iterator it;
            it=s.find(5);    //查找键值为5的元素
            if(it!=s.end())    //找到
                cout<<*it<<endl;
            else            //未找到
                cout<<"未找到";
6.自定义比较函数
    (1)元素不是结构体:
        例:
        //自定义比较函数myComp,重载“()”操作符
        struct myComp
        {
            bool operator()(const your_type &a,const your_type &b)
            [
                return a.data-b.data>0;
            }
        }
        set<int,myComp>s;
        ......
        set<int,myComp>::iterator it;
    (2)如果元素是结构体,可以直接将比较函数写在结构体内
        例:
        struct Info
        {
            string name;
            float score;
            //重载“<”操作符,自定义排序规则
            bool operator < (const Info &a) const
            {
                //按score从大到小排列
                return a.score<score;
            }
        }
        set<Info> s;
        ......
        set<Info>::iterator it;

C++STL1--set的更多相关文章

  1. POJ C++程序设计 编程题#1 编程作业—STL1

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出结 ...

  2. stl1

    #include<iostream> #include<map> #include<string> using namespace std;   map<st ...

  3. python打包成exe(py2exe)

    对比了几个打包工具,发现py2exe更好用一点,一个命令就可以. 1.获取 http://prdownloads.sourceforge.net/py2exe 下载python版本对应的.直接下载然后 ...

  4. 微信小程序(五) 利用模板动态加载数据

    利用模板动态加载数据,其实是对上一节静态数据替换成动态数据:

  5. [USACO5.1]夜空繁星Starry Night

    题目背景 高高的星空,簇簇闪耀的群星形态万千.一个星座(cluster)是一群连通的星组成的非空连通星系,这里的连通是指水平,垂直或者对角相邻的两个星星.一个星座不能是另一个更大星座的一部分, 星座可 ...

  6. C++Review7_STL、容器、迭代器

    我之前的博文中有专门的5篇整理并介绍了STL的概念: STL1——整体介绍:https://www.cnblogs.com/grooovvve/p/10467794.html STL2——泛型编程(模 ...

  7. $vjudge\ CSP-S$专题专练题解

    照例先放个链接$QwQ$ $A$ $QwQ$之前写过题解辣. 重新说下趴,就给横坐标纵坐标也开点,然后每个点连向对应横纵坐标边权为$0$,相邻横坐标点之间连边,相邻纵坐标点之间连边,跑个最短路就完事$ ...

  8. $bzoj4152\ The\ Captain$ 最短路

    正解:最短路+优化连边 解题报告: 传送门$w$ 这种优化连边啥的真的好妙噢$QwQ$ 首先显然离散化下不说$QwQ$.然后对所有横坐标纵坐标分别建点,相邻两横坐标点相连,边权为离散前的坐标差.纵坐标 ...

  9. C++记录(二)

    1.算术移位和逻辑移位. 逻辑移位是只补0,算术移位是看符号,负数补1,正数补0(讨论的是右移的情况下). 负数左移右边一样补0.如果遇到位运算的相关题目需要对int变量进行左移而且不知道正负,那么先 ...

  10. C++-标准模板库

    C++较之C语言强大的功能之一是,C++编译器自带了大量的可复用代码库,我们称为标准模板库(standard template library),STL.标准模板库是一套常用的数据结构的集合,包括链表 ...

随机推荐

  1. libsvm java版本使用心得(转)

    http://blog.csdn.net/u010340854/article/details/19159883 https://github.com/cjlin1/libsvm 项目中要用到svm分 ...

  2. linux服务器---安装swat

    安装swat swat是一个图形化的samba管理软件,可以帮助不熟悉的人去灵活的配置samba服务, 1.安装swat [root@localhost wj]#yum install -y samb ...

  3. oracle创建dblink的脚本

    创建dblink的脚本 create public database link wsbsbb_27(dblink名字) connect to wsbsbb(用户名) IDENTIFIED BY wsb ...

  4. MySQL数据库总结

    引擎 查看MySQL默认引擎:show variables like '%storage_engine%'; 查看表引擎:show table status from 数据库名; 修改表引擎alter ...

  5. Total Difference String

    Total Difference Strings 给一个string列表,判断有多少个不同的string,返回个数相同的定义:字符串长度相等并从左到右,或从右往左是同样的字符 abc 和 cba 为视 ...

  6. nginx location正则写法

    nginx location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # ...

  7. js面向对象编程: js类定义函数时prototype和this区别?

    参考文章的链接:http://www.2cto.com/kf/201406/307790.html 测试代码如下: function ListCommon2(afirst) { var first=a ...

  8. Jackson 使用和注意项

    依赖maven: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId ...

  9. [BZOJ4244]邮戳拉力赛

    Description IOI铁路是由N+2个站点构成的直线线路.这条线路的车站从某一端的车站开始顺次标号为0...N+1. 这条路线上行驶的电车分为上行电车和下行电车两种,上行电车沿编号增大方向行驶 ...

  10. SpringJDBC源码分析记录

    我们使用JdbcTemplate时,调用的query方法为: public <T> List<T> query(String sql, @Nullable Object[] a ...