C++进阶-3-5-set/multiset容器

  1 #include<iostream>
2 #include<set>
3 using namespace std;
4
5 // set/multiset容器
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 // 1.构造和赋值
17 void test01() {
18
19 set<int> s1;
20
21 // 插入数据,只有insert
22 s1.insert(10);
23 s1.insert(40);
24 s1.insert(30);
25 s1.insert(20);
26 s1.insert(30);
27
28 // 元素插入,自动排序,且不运行有重复数据
29 printSet(s1);
30
31 // 拷贝构造
32 set<int>s2(s1);
33 printSet(s2);
34
35 // 赋值
36 set<int>s3;
37 s3 = s2;
38 printSet(s3);
39
40 }
41
42 // 2.大小和交换
43 void test02() {
44
45 set<int> s1;
46
47 s1.insert(10);
48 s1.insert(30);
49 s1.insert(20);
50 s1.insert(40);
51
52 printSet(s1);
53
54 // 判断是否为空
55 if (s1.empty()) {
56 cout << "s1为空" << endl;
57 }
58 else {
59 cout << "s1不为空" << endl;
60 cout << "s1的大小为:" << s1.size() << endl;
61 }
62
63 // 交换
64 set<int> s2;
65
66 s2.insert(100);
67 s2.insert(300);
68 s2.insert(200);
69 s2.insert(400);
70
71 cout << "交换前:" << endl;
72 printSet(s1);
73 printSet(s2);
74
75 cout << "交换后:" << endl;
76 s1.swap(s2);
77 printSet(s1);
78 printSet(s2);
79
80 }
81
82 // 3.插入和删除
83 void test03() {
84
85 set<int> s1;
86
87 // 插入
88 s1.insert(10);
89 s1.insert(30);
90 s1.insert(20);
91 s1.insert(40);
92
93 // 遍历
94 printSet(s1);
95
96 // 删除
97 s1.erase(s1.begin());
98 printSet(s1);
99
100 // 删除重载版本
101 s1.erase(30);
102 printSet(s1);
103
104 // 清空
105 //s1.erase(s1.begin(), s1.end())
106 s1.clear();
107 printSet(s1);
108
109 }
110
111 // 4.查找和统计
112 void test04() {
113
114 set<int> s1;
115
116 s1.insert(10);
117 s1.insert(30);
118 s1.insert(20);
119 s1.insert(40);
120
121 printSet(s1);
122
123 // 查找
124 // 存在,返回该元素的迭代器,不存在,返回set.end();
125
126 set<int>::iterator pos = s1.find(30);
127 if (pos != s1.end()) {
128 cout << "找到元素" << endl;
129 }
130 else
131 {
132 cout << "未找到元素" << endl;
133 }
134
135 // 统计
136 int num = s1.count(40); // 统计40的个数
137 cout << "num = " << num << endl;
138 // 对于set而言,统计结果,要么是0, 要么是1
139
140 }
141
142 // 5. set和multiset区别
143 void test05() {
144
145 set<int> s;
146
147 // set插入数据的同时还会返回插入的结果,表示插入是否成功
148 pair<set<int>::iterator, bool>set = s.insert(10);
149
150 if (set.second) {
151 cout << "第一次插入成功!" << endl;
152 }
153 else
154 {
155 cout << "第一次插入失败!" << endl;
156 }
157
158 set = s.insert(10);
159
160 if (set.second) {
161 cout << "第二次插入成功!" << endl;
162 }
163 else
164 {
165 cout << "第二次插入失败!" << endl;
166 }
167
168
169 // multiset允许插入重复值
170 multiset<int>ms;
171 ms.insert(10);
172 ms.insert(10);
173 ms.insert(10);
174
175 for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
176 cout << *it << " ";
177 }
178 cout << endl;
179 }
180
181
182 // 6.pair对组创建
183 void test06() {
184
185 // pair对组,成对出现的数据,利用对组可以返回两个数据
186
187 // 第一种方式
188 pair<string, int>p(string("Tom"), 20);
189 cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
190
191 // 第二种数据
192 pair<string, int>p2 = make_pair("Jerry", 30);
193 cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
194
195 }
196
197 // 7.set容器排序
198
199 class MyCompare {
200 public:
201 // 仿函数,重写(),类似于函数
202 bool operator()(int v1, int v2) {
203 return v1 > v2;
204 }
205 };
206
207 void test07() {
208
209 // set排序是由小到大的升序
210 // 利用仿函数,可以改变排序规则
211
212 set<int> s1;
213
214 s1.insert(10);
215 s1.insert(30);
216 s1.insert(20);
217 s1.insert(40);
218
219 printSet(s1);
220
221 // 改为从大到小,在插入之前操作
222
223 set<int, MyCompare> s2;
224
225 s2.insert(10);
226 s2.insert(30);
227 s2.insert(20);
228 s2.insert(40);
229
230 for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
231 {
232 cout << *it << " ";
233 }
234 cout << endl;
235 }
236
237
238 int main() {
239
240 // 1.构造和赋值
241 //test01();
242
243 // 2.大小和交换
244 //test02();
245
246 // 3.插入和删除
247 //test03();
248
249 // 4.查找和统计
250 //test04();
251
252 // 5. set和multiset区别
253 //test05();
254
255 // 6.pair对组创建
256 //test06();
257
258 // 7.set容器排序--内置数据类型
259 //test07();
260
261 system("pause");
262
263 return 0;
264 }
265
266 // 总结
267 //
268 // set/multiset容器
269 //
270 // 简介:所有元素都会在插入时自动排序
271 //
272 // 本质:set/multiset属于关联式容器,底层结构是二叉树实现
273 //
274 // set/multiset区别:
275 // 1.set不允许容器中有重复的元素
276 // 2.multiset允许容器中有重复的元素
277 // 3.set插入数据的同时还会返回插入的结果,表示插入是否成功
278 // 4.multiset不会检测数据,因此可以插入重复数据
279 //

C++进阶-3-5-set/multiset容器的更多相关文章

  1. STL学习系列八:Set和multiset容器

    1.set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实 ...

  2. STL之Set和multiset容器

    1.Set和multiset容器 1)set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. 2)set采用红黑树变体的数据 ...

  3. C++ STL 学习笔记__(7)Set和multiset容器

    10.2.8 Set和multiset容器 set/multiset的简介 ²  set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指 ...

  4. STL Set和multiset 容器

    STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...

  5. 第十三篇:multimap容器和multiset容器中的find操作

    前言 multimap容器是map容器的“ 增强版 ”,它允许一个键对应多个值.对于map容器来说,find函数将会返回第一个键值匹配元素所在处的迭代器.那么对于multimap容器来说,find函数 ...

  6. multimap容器和multiset容器中的find操作

    前言 multimap容器是map容器的“ 增强版 ”,它允许一个键对应多个值.对于map容器来说,find函数将会返回第一个键值匹配元素所在处的迭代器.那么对于multimap容器来说,find函数 ...

  7. set和multiset容器

    set和multiset容器的能力 set 和multiset容器的内部结构通常由平衡二叉树(balanced binary tree)来实现.当元素放入容器中时,会按照一定的排序法则自动排序,默认是 ...

  8. stl之multiset容器的应用

    与set集合容器一样,multiset多重集合容器也使用红黑树组织元素数据,仅仅是multiset容器同意将反复的元素健值插入.而set容器则不同意. set容器所使用的C++标准头文件set.事实上 ...

  9. 详解C++ STL multiset 容器

    详解C++ STL multiset 容器 本篇随笔简单介绍一下\(C++STL\)中\(multiset\)容器的使用方法及常见使用技巧. multiset容器的概念和性质 \(set\)在英文中的 ...

随机推荐

  1. Java 中 LinkedHashMap 和 PriorityQueue 的区别是 什么?

    PriorityQueue 保证最高或者最低优先级的的元素总是在队列头部,但是 LinkedHashMap 维持的顺序是元素插入的顺序.当遍历一个 PriorityQueue 时,没有任何顺序保证,但 ...

  2. getch()函数的使用方法及其返回值问题

    getch()函数依赖于头文件 conio.h .会在windows平台下从控制台无回显地取一个字符,并且返回读取到的字符. 然而,我在实际用这个函数才发现getch()这个函数并不简单. getch ...

  3. [译] Facebook:我们是如何构建第一个跨平台的 React Native APP

    英文原文(需FQ):https://code.facebook.com/posts/1189117404435352/ 早些时候,我们介绍过iOS版的React Native. React Nativ ...

  4. 移动端调试工具weinre安装教程(java版)

    先申明:本安装教程是基于java的jdk安装的,经过测试可以正常使用,基于nodejs的安装,小喵鼓弄了好几天也没有成功,如果哪位童鞋基于nodejs安装成功了,请联系小喵,小喵在这里先谢谢你了! 好 ...

  5. 给一个非矩形数组(Nonrectangular Arrays)

    Nonrectangular Arrays(非矩形数组)  public class Test {     public static void main(String[] args) {       ...

  6. springAop必导jar包

    SpringAop:的底层就是通过JDK动态代理"或"CGLib动态代理为技术目标织入横切逻辑. 做aop:需要导入: spring-aop-4.1.5.RELEASE.jar s ...

  7. MySQL 中的 SQL 语句详解

    @ 目录 总结内容 1. 基本概念 2. SQL列的常用类型 3. DDL简单操作 3.1 数据库操作 3.2 表操作 4. DML操作 4.1 修改操作(UPDATE SET) 4.2 插入操作(I ...

  8. java集合总览

    在编写java程序中,我们最常用的除了八种基本数据类型,String对象外还有一个集合类,在我们的的程序中到处充斥着集合类的身影!java中集合大家族的成员实在是太丰富了,有常用的ArrayList. ...

  9. script标签中defer和async的区别(稀土掘金学习)

    如果没有defer或async属性,浏览器会立即加载并执行相应的脚本.它不会等待后续加载的文档元素,读取到就会开始加载和执行,这样就阻塞了后续文档的加载. 下图可以直观的看出三者之间的区别: 其中蓝色 ...

  10. git概述

    学习资料来源-人家写得比我好 #视频教程: https://www.bilibili.com/video/BV1vy4y1s7k6?spm_id_from=pageDriver #文档教程 https ...