set集合容器实现了红黑树(Red-Black Tree)的平衡二叉检索树的的数据结构,在插入元素时,它会自动调整二叉树的排列,把该元素放到适当的位置,以确保每个子树根节点的键值大于左子树所有节点的键值,而小于右子树所有节点的键值;另外,还得确保根节点的左子树的高度与有字数的高度相等,这样,二叉树的高度最小,从而检索速度最快。要注意的是,它不会重复插入相同键值的元素,而采取忽略处理。

平衡二叉检索树的检索使用中序遍历算法,检索效率高于vector、deque、和list的容器。另外,采用中序遍历算法可将键值由小到大遍历出来,所以,可以理解为平衡二叉检索树在插入元素时,就会自动将元素按键值从小到大的顺序排列。

构造set集合的主要目的是为了快速检索,使用set前,需要在程序头文件中包含声明“#include<set>”。

1.创建set集合对象

创建set对象时,需要指定元素的类型,这一点和其他容器一样。

  1. #include<iostream>
  2. #include<set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. return 0;
  8. }

2.元素的插入与中序遍历

采用inset()方法把元素插入到集合中,插入规则在默认的比较规则下,是按元素值从小到大插入,如果自己指定了比较规则函数,则按自定义比较规则函数插入。使用前向迭代器对集合中序遍历,结果正好是元素排序后的结果。

  1. #include<iostream>
  2. #include<set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. s.insert(5); //第一次插入5,可以插入
  8. s.insert(1);
  9. s.insert(6);
  10. s.insert(3);
  11. s.insert(5); //第二次插入5,重复元素,不会插入
  12. set<int>::iterator it; //定义前向迭代器
  13. //中序遍历集合中的所有元素
  14. for(it = s.begin(); it != s.end(); it++)
  15. {
  16. cout << *it << " ";
  17. }
  18. cout << endl;
  19. return 0;
  20. }
  21. //运行结果:1 3 5 6

3.元素的方向遍历

使用反向迭代器reverse_iterator可以反向遍历集合,输出的结果正好是集合元素的反向排序结果。它需要用到rbegin()和rend()两个方法,它们分别给出了反向遍历的开始位置和结束位置。

  1. #include<iostream>
  2. #include<set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. s.insert(5); //第一次插入5,可以插入
  8. s.insert(1);
  9. s.insert(6);
  10. s.insert(3);
  11. s.insert(5); //第二次插入5,重复元素,不会插入
  12. set<int>::reverse_iterator rit; //定义反向迭代器
  13. //反向遍历集合中的所有元素
  14. for(rit = s.rbegin(); rit != s.rend(); rit++)
  15. {
  16. cout << *rit << " ";
  17. }
  18. cout << endl;
  19. return 0;
  20. }
  21. //运行结果:6 5 3 1

4.元素的删除

与插入元素的处理一样,集合具有高效的删除处理功能,并自动重新调整内部的红黑树的平衡。删除的对象可以是某个迭代器位置上的元素、等于某键值的元素、一个区间上的元素和清空集合。

  1. #include<iostream>
  2. #include<set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. s.insert(5); //第一次插入5,可以插入
  8. s.insert(1);
  9. s.insert(6);
  10. s.insert(3);
  11. s.insert(5); //第二次插入5,重复元素,不会插入
  12. s.erase(6); //删除键值为6的元素
  13. set<int>::reverse_iterator rit; //定义反向迭代器
  14. //反向遍历集合中的所有元素
  15. for(rit = s.rbegin(); rit != s.rend(); rit++)
  16. {
  17. cout << *rit << " ";
  18. }
  19. cout << endl;
  20. set<int>::iterator it;
  21. it = s.begin();
  22. for(int i = 0; i < 2; i++)
  23. it = s.erase(it);
  24. for(it = s.begin(); it != s.end(); it++)
  25. cout << *it << " ";
  26. cout << endl;
  27. s.clear();
  28. cout << s.size() << endl;
  29. return 0;
  30. }
  31. /*
  32. 运行结果:
  33. 5 3 1
  34. 5
  35. 0
  36. */

5.元素的检索

使用find()方法对集合进行检索,如果找到查找的的键值,则返回该键值的迭代器位置;否则,返回集合最后一个元素后面的一个位置,即end()。

  1. #include<iostream>
  2. #include<set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. s.insert(5); //第一次插入5,可以插入
  8. s.insert(1);
  9. s.insert(6);
  10. s.insert(3);
  11. s.insert(5); //第二次插入5,重复元素,不会插入
  12. set<int>::iterator it;
  13. it = s.find(6); //查找键值为6的元素
  14. if(it != s.end())
  15. cout << *it << endl;
  16. else
  17. cout << "not find it" << endl;
  18. it = s.find(20);
  19. if(it != s.end())
  20. cout << *it << endl;
  21. else
  22. cout << "not find it" << endl;
  23. return 0;
  24. }
  25. /*
  26. 运行结果:
  27. 6
  28. not find it
  29. */

下面这种方法也能判断一个数是否在集合中:

  1. #include <cstdio>
  2. #include <set>
  3. using namespace std;
  4. int main() {
  5. set <int> s;
  6. int a;
  7. for(int i = 0; i < 10; i++)
  8. s.insert(i);
  9. for(int i = 0; i < 5; i++) {
  10. scanf("%d", &a);
  11. if(!s.count(a)) //不存在
  12. printf("does not exist\n");
  13. else
  14. printf("exist\n");
  15. }
  16. return 0;
  17. }

6.自定义比较函数

使用insert将元素插入到集合中去的时候,集合会根据设定的比较函数奖该元素放到该放的节点上去。在定义集合的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值从小到大的顺序插入元素。但在很多情况下,需要自己编写比较函数。

编写比较函数有两种方法。

(1)如果元素不是结构体,那么可以编写比较函数。下面的程序比较规则为按键值从大到小的顺序插入到集合中。

  1. #include<iostream>
  2. #include<set>
  3. using namespace std;
  4. struct mycomp
  5. { //自定义比较函数,重载“()”操作符
  6. bool operator() (const int &a, const int &b)
  7. {
  8. if(a != b)
  9. return a > b;
  10. else
  11. return a > b;
  12. }
  13. };
  14. int main()
  15. {
  16. set<int, mycomp> s; //采用比较函数mycomp
  17. s.insert(5); //第一次插入5,可以插入
  18. s.insert(1);
  19. s.insert(6);
  20. s.insert(3);
  21. s.insert(5); //第二次插入5,重复元素,不会插入
  22. set<int,mycomp>::iterator it;
  23. for(it = s.begin(); it != s.end(); it++)
  24. cout << *it << " ";
  25. cout << endl;
  26. return 0;
  27. }
  28. /*
  29. 运行结果:6 5 3 1
  30. */

(2)如果元素是结构体,那么可以直接把比较函数写在结构体内。

    1. #include<iostream>
    2. #include<set>
    3. #include<string>
    4. using namespace std;
    5. struct Info
    6. {
    7. string name;
    8. double score;
    9. bool operator < (const Info &a) const // 重载“<”操作符,自定义排序规则
    10. {
    11. //按score由大到小排序。如果要由小到大排序,使用“>”即可。
    12. return a.score < score;
    13. }
    14. };
    15. int main()
    16. {
    17. set<Info> s;
    18. Info info;
    19. //插入三个元素
    20. info.name = "Jack";
    21. info.score = 80;
    22. s.insert(info);
    23. info.name = "Tom";
    24. info.score = 99;
    25. s.insert(info);
    26. info.name = "Steaven";
    27. info.score = 60;
    28. s.insert(info);
    29. set<Info>::iterator it;
    30. for(it = s.begin(); it != s.end(); it++)
    31. cout << (*it).name << " : " << (*it).score << endl;
    32. return 0;
    33. }
    34. /*
    35. 运行结果:
    36. Tom : 99
    37. Jack : 80
    38. Steaven : 60
    39. */

STL之set集合容器 【转】的更多相关文章

  1. 【STL】 set集合容器常用用法

    set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...

  2. C++STL之set集合容器

    set集合容器 set集合容器实现了红黑树(Red-Black Tree)的平衡二叉检索树的数据结构, 在 插入元素时, 它会自动调整二叉树的排列, 把该元素放到适当的位置, 以确保每个子树根节点的键 ...

  3. stl之set集合容器应用基础

    set集合容器使用一种称为红黑树(Red-Black Tree) 的平衡二叉检索树的数据结构,来组织泛化的元素数据.每一个节点包括一个取值红色或黑色的颜色域.以利于进行树的平衡处理.作为节点键值的元素 ...

  4. multiset多重集合容器(常用的使用方法总结)

    关于C++STL中multiset集合容器的学习,看别人的代码一百遍,不如自己动手写一遍. multiset多重集合容器和set集合容器的使用方法大多相同,不同的是multiset多重集合容器允许重复 ...

  5. set集合容器(常用的使用方法总结)

     关于C++STL中set集合容器的学习,看别人的代码一百遍,不如自己动手写一遍. 构造set集合容器的目的是为了去重+排序+快速搜索.由于set集合容器实现了红黑树多的平衡二叉检索树的数据结构,在插 ...

  6. C++ STL set集合容器

    汇总了一些set的常用语句,部分参考了这篇:http://blog.163.com/jackie_howe/blog/static/199491347201231691525484/ #include ...

  7. STL中的set集合容器进行集合运算:并、交、差实例

    集合容器的集合运算:并.交.差: #include "stdafx.h" #include <iostream> #include <set> #inclu ...

  8. C++STL之multiset多重集合容器

    multiset多重集合容器 multiset与set一样, 也是使用红黑树来组织元素数据的, 唯一不同的是, multiset允许重复的元素键值插入, 而set则不允许. multiset也需要声明 ...

  9. STL中的set容器的一点总结

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

随机推荐

  1. mp4文件数据格式解析

    unsigned int(32)[3]    32*3bit string[32]  32*8bit class VisualSampleEntry(codingname) extends Sampl ...

  2. 如何制作RTS游戏的寻路系统?

    Q1:我们在做一个RTS游戏,开始用的是Unity自带的NavMesh的寻路,但发现这个并不适合RTS多人寻路,因为总会出现阻挡和闪跳的问题.看Asset Store上的A* path插件评论说在碰撞 ...

  3. 洛谷——P1330 封锁阳光大学

    P1330 封锁阳光大学 题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构 ...

  4. 打开tcp_tw_recycle引起的一个问题

    今天普空说了一个问题就是如果设置了tcp_tw_recycle ,那么如果客户端是NAT出来的,那么就可能会出现连接被直接rst的情况.然后我google了下,在内核列表也有人说了这个问题 https ...

  5. HDU2874【倍增、ST】

    题目链接[https://vjudge.net/problem/HDU-2874] 题意: 输入一个森林,总节点不超过N(N<10000),由C次询问(C<1000000),每次询问两个点 ...

  6. 【BZOJ 3534】 3534: [Sdoi2014]重建 (Matrix-Tree Theorem)

    3534: [Sdoi2014]重建 Time Limit: 10 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 709  Solved: 32 ...

  7. FFTW3学习笔记3:FFTW 和 CUFFT 的使用对比

    一.流程 1.使用cufftHandle创建句柄 2.使用cufftPlan1d(),cufftPlan3d(),cufftPlan3d(),cufftPlanMany()对句柄进行配置,主要是配置句 ...

  8. 又见Python<2>:如何安装第三方库(Windows)

    使用python进行数据分析或者数据处理时,往往需要使用一些库,而使用库之前必须安装它.Anaconda内置了很多常用的第三方库,可以满足绝大部分需求,比如numpy.pandas.matplotli ...

  9. javaWeb导出POI创建的多个excel的压缩文件

    文件效果图: 接口代码: //测试 http://localhost:8080/admin/test/test/poizip @RequestMapping(value = "/poizip ...

  10. 中国剩余定理 hdu 3579

    HDU 3579 Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...