set/multiset 的特性是所有元素会根据元素的值自动进行排序。set 是以 RB-tree(红黑树,平衡二叉树的一种)为底层机制,其查找效率非常好。set 容器中不允许重复元
素,multiset 允许重复元素。
我们可以通过 set 的迭代器改变元素的值吗?
答: 不行,因为 set 集合是根据元素值进行排序,关系到 set 的排序规则,如果任意改变 set 的元素值,会严重破坏 set 组织。
  1. #include <iostream>
  2. #include <set>
  3. #include <list>
  4. #include <string>
  5. using namespace std;
  6.  
  7. void PrintSet(set<int>& s)
  8. {
  9. for (set<int>::iterator it = s.begin(); it != s.end(); it++)
  10. {
  11. cout << *it << " ";
  12. }
  13. cout << endl;
  14. }
  15.  
  16. class mycompare
  17. {
  18. public:
  19. bool operator()(int v1, int v2)
  20. {
  21. return v1 > v2;
  22. }
  23. };
  24.  
  25. // set初始化
  26. // set<T> st;//set 默认构造函数:
  27. // mulitset<T> mst; //multiset 默认构造函数:
  28. // set(const set &st);//拷贝构造函数
  29. void test01()
  30. {
  31. set<int> s1; // 自动进行排序, 默认从小到大
  32. s1.insert();
  33. s1.insert();
  34. s1.insert();
  35. s1.insert();
  36. s1.insert();
  37. PrintSet(s1);
  38. // 赋值操作
  39. // set& operator=(const set &st);//重载等号操作符
  40. // swap(st);//交换两个集合容器
  41. set<int> s2;
  42. s2 = s1;
  43. PrintSet(s2);
  44. // 删除操作
  45. // insert(elem);//在容器中插入元素。
  46. // clear();//清除所有元素
  47. // erase(pos);//删除 pos 迭代器所指的元素,返回下一个元素的迭代器。
  48. // erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
  49. // erase(elem);//删除容器中值为 elem 的元素。
  50. s1.erase(s1.begin());
  51. s1.erase();
  52. PrintSet(s1);
  53. cout << "----------------------" << endl;
  54. }
  55.  
  56. // set查找
  57. // find(key);//查找键 key 是否存在,若存在,返回该键的元素的迭代器;若不存在,返回 map.end();
  58. // lower_bound(keyElem);//返回第一个 key>=keyElem 元素的迭代器。
  59. // upper_bound(keyElem);//返回第一个 key>keyElem 元素的迭代器。
  60. // equal_range(keyElem);//返回容器中 key 与 keyElem 相等的上下限的两个迭代器。
  61. void test02()
  62. {
  63. set<int> s1;
  64. s1.insert();
  65. s1.insert();
  66. s1.insert();
  67. s1.insert();
  68. s1.insert();
  69. set<int>::iterator ret = s1.find();
  70. if (ret == s1.end())
  71. {
  72. cout << "没有找到!" << endl;
  73. }
  74. else
  75. {
  76. cout << "ret: " << *ret << endl;
  77. }
  78. // 找第一个大于key的值
  79. ret = s1.upper_bound();
  80. if (ret == s1.end())
  81. {
  82. cout << "没有找到!" << endl;
  83. }
  84. else
  85. {
  86. cout << "ret: " << *ret << endl;
  87. }
  88. // equal_range 返回Lower_bound 和 upper_bound值
  89. pair<set<int>::iterator, set<int>::iterator> myret = s1.equal_range();
  90. if (myret.first == s1.end())
  91. {
  92. cout << "没有找到!" << endl;
  93. }
  94. else
  95. {
  96. cout << "myret: " << *(myret.first) << endl;
  97. }
  98. if (myret.second == s1.end())
  99. {
  100. cout << "没有找到!" << endl;
  101. }
  102. else
  103. {
  104. cout << "myret: " << *(myret.second) << endl;
  105. }
  106. cout << "----------------" << endl;
  107. }
  108.  
  109. class Person
  110. {
  111. public:
  112. Person(int age, int id) :id(id), age(age){}
  113. public:
  114. int id;
  115. int age;
  116. };
  117.  
  118. class mycompare2
  119. {
  120. public:
  121. bool operator()(Person p1, Person p2)
  122. {
  123. if (p1.id == p2.id)
  124. {
  125. return p1.age > p2.age;
  126. }
  127. else
  128. {
  129. p1.id > p2.id;
  130. }
  131. }
  132. };
  133.  
  134. void test03()
  135. {
  136. set<Person, mycompare2> sp;
  137. Person p1(, ), p2(, ), p3(, );
  138. sp.insert(p1);
  139. sp.insert(p2);
  140. sp.insert(p3);
  141. Person p4(, );
  142. for (set<Person, mycompare2>::iterator it = sp.begin(); it != sp.end(); it++)
  143. {
  144. cout << (*it).age << " " << (*it).id << endl;
  145. }
  146. set<Person, mycompare2>::iterator ret = sp.find(p4);
  147. if (ret == sp.end())
  148. {
  149. cout << "没有找到!" << endl;
  150. }
  151. else
  152. {
  153. cout << "找到:" << (*ret).id << " " << (*ret).age << endl;
  154. }
  155. }
  156.  
  157. // 对组
  158.  
  159. void test04()
  160. {
  161. // 构造方法
  162. pair<int, int> pair1(, );
  163. cout << pair1.first << " " << pair1.second << endl;
  164. pair<int, string> pair2 = make_pair(, "aaaaa");
  165. cout << pair2.first << " " << pair2.second << endl;
  166. pair<int, string> pair3 = pair2;
  167. }
  168.  
  169. int main()
  170. {
  171. test01();
  172. test02();
  173. test03();
  174. test04();
  175. getchar();
  176. return ;
  177. }

C++ STL 之 set 和 pair的更多相关文章

  1. STL之map与pair与unordered_map常用函数详解

    STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...

  2. stl(set和pair)

    D - 4 Gym - 100989D In this cafeteria, the N tables are all ordered in one line, where table number ...

  3. UVA 10763 Foreign Exchange 出国交换 pair+map

    题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...

  4. make_pair

    Utilities <utility> 由短小精干的类和函数构成,执行最一般性的工作. 这些工具包括: general types 一些重要的C函数 numeric limits Pair ...

  5. Codeforces Round 500 (Div 2) Solution

    从这里开始 题目地址 瞎扯 Problem A Piles With Stones Problem B And Problem C Photo of The Sky Problem D Chemica ...

  6. 【转】c++ make_pair函数使用

    [好记性不如烂笔头:在<C++ Templates>看到这个函数,发现正是前段时间写项目程序所要用到的,可惜当时还不知道有这个用法,当时是自己写了个结构体..]Utilities < ...

  7. C++11--Tuple类<tuple>

    #include "stdafx.h" #include <iomanip> #include <condition_variable> #include ...

  8. 洛谷 P1954 [NOI2010]航空管制

    https://www.luogu.org/problemnew/show/P1954 拓扑排序, 注意到如果正着建图("a出现早于b"=>"a向b连边" ...

  9. STL的pair学习, map学习

    http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...

随机推荐

  1. kotlin使用中辍标记法调用函数

    fun main(arg: Array<String>) { var str = "hello world" print(str div("l")) ...

  2. Linux与linux之间传递文件、

    1.从linux本机文件上传到另一台linux格式:scp 要传的文件 root@目标ip:路径scp –r 要传的目录 root@目标ip:路径 例子: scp  /root/1.txt   roo ...

  3. C#与C++数据类型对应表(搜集整理一)

    C#与C++数据类型对应表(搜集整理一) C#与C++数据类型对应表   C#调用DLL文件时参数对应表 Wtypes.h 中的非托管类型 非托管 C 语言类型 托管类名 说明 HANDLE void ...

  4. Docker save and load,镜像保存

    一.起因 docker pull 时发生错误 error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/r ...

  5. php-fpm 优化

    /usr/local/php/etc/php-fpm.conf 优化 [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /us ...

  6. 生产者-消费者问题与quene模块

    生产者-消费者问题与quene模块 下面使用线程锁以及队列来模拟一个典型的案例:生产者-消费者模型.在这个场景下,商品或服务的生产者生产商品,然后将其放到类似队列的数据结构中,生产商品的时间是不确定的 ...

  7. 利用官方的ucosiii包中测试板的工程移植到属于自己的开发板(stmf103ZE)上

    ucosIII官方下载地址:https://www.micrium.com 第一:是不是ucosIII:第二,工具链是不是keil(我用的是keil,如何用的是IAR就选有IAR的):第三MCU是不是 ...

  8. jquery中的$(document).ready(function(){})和$(window).load()比较

    1.执行时间 window.onload()即jquery写法中的$(window).load(function(){})必须等到页面内包括图片的所有元素加载完毕后才能执行. $(document). ...

  9. Milo-OPC UA处理Subscription和Triggering

    Subscription有两种模式,一种是Reporting,另一种是Sampling. 如果定义为Sampling,则这个Subscription是一个Triggered Item,即被激发的订阅, ...

  10. 2019-10-20 李宗盛 linux

    Linux Linux简介(了解) Linux介绍:Linux是类UNIX计算机的统称 Linux操作系统的内核也是Linux Linux是由芬兰大学生Linux Torvalds于1991年编写 L ...