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. 如果我不输入<!DOCTYPE HTML>,HTML 5能工作吗?

    No,浏览器将无法识别HTML文件,并且HTML 5标签将无法正常工作.

  2. 抽象的(abstract)方法是否可同时是静态的(static), 是否可同时是本地方法(native),是否可同时被 synchronized 修饰?

    都不能.抽象方法需要子类重写,而静态的方法是无法被重写的,因此二者是矛 盾的.本地方法是由本地代码(如 C 代码)实现的方法,而抽象方法是没有实现 的,也是矛盾的.synchronized 和方法的实 ...

  3. Tcp的Flags

    在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN, FIN, ACK, PSH, RST, URG. 其中,对于我们日常的分析有用的就是前面的五个字段. 它们的含义是: SYN表示建立连 ...

  4. 学习GlusterFS(六)

    一.GlusterFS概述 分布式文件系统由来 在介绍之前我们先来看下文件系统及典型的NFS文件系统. 计算机通过文件系统管理,存储数据的.而现在数据信息时代中人们可获取数据成指数倍的增长,单纯通过增 ...

  5. Spring系列28:@Transactional事务源码分析

    本文内容 @Transactional事务使用 @EnableTransactionManagement 详解 @Transactional事务属性的解析 TransactionInterceptor ...

  6. numpy入门—numpy是什么

    numpy是什么?为什么使用numpy 使用numpy库与原生python用于数组计算性能对比

  7. 顺利通过EMC实验(5)

  8. Azure DevOps 中 Dapr项目自动部署流程实践

    注:本文中主要讨论 .NET6.0项目在 k8s 中运行的 Dapr 的持续集成流程, 但实际上不是Dapr的项目部署到K8s也是相同流程,只是k8s的yaml配置文件有所不同 流程选择 基于 Dap ...

  9. JavaScript实现简单轮播图动画

    运行效果: 源代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset=&quo ...

  10. vue报错解决方案

    Vue build faild 解决办法: https://blog.csdn.net/u011169370/article/details/83346176 ? jbcmVideo git:(oah ...