C++进阶-3-5-set/multiset容器
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容器的更多相关文章
- STL学习系列八:Set和multiset容器
1.set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实 ...
- STL之Set和multiset容器
1.Set和multiset容器 1)set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. 2)set采用红黑树变体的数据 ...
- C++ STL 学习笔记__(7)Set和multiset容器
10.2.8 Set和multiset容器 set/multiset的简介 ² set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指 ...
- STL Set和multiset 容器
STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...
- 第十三篇:multimap容器和multiset容器中的find操作
前言 multimap容器是map容器的“ 增强版 ”,它允许一个键对应多个值.对于map容器来说,find函数将会返回第一个键值匹配元素所在处的迭代器.那么对于multimap容器来说,find函数 ...
- multimap容器和multiset容器中的find操作
前言 multimap容器是map容器的“ 增强版 ”,它允许一个键对应多个值.对于map容器来说,find函数将会返回第一个键值匹配元素所在处的迭代器.那么对于multimap容器来说,find函数 ...
- set和multiset容器
set和multiset容器的能力 set 和multiset容器的内部结构通常由平衡二叉树(balanced binary tree)来实现.当元素放入容器中时,会按照一定的排序法则自动排序,默认是 ...
- stl之multiset容器的应用
与set集合容器一样,multiset多重集合容器也使用红黑树组织元素数据,仅仅是multiset容器同意将反复的元素健值插入.而set容器则不同意. set容器所使用的C++标准头文件set.事实上 ...
- 详解C++ STL multiset 容器
详解C++ STL multiset 容器 本篇随笔简单介绍一下\(C++STL\)中\(multiset\)容器的使用方法及常见使用技巧. multiset容器的概念和性质 \(set\)在英文中的 ...
随机推荐
- SVG是什么?
SVG表示(scalable vector graphics)可缩放矢量图形.这是一个基于文本的图形语言,它可以绘制使用文本.线.点等的图形,因此可以轻巧又快速地渲染.
- 数据分析之Pandas操作
Pandas pandas需要导入 import pandas as pd from pandas import Series,DataFrame import numpy as np 1 Serie ...
- mybatis-01-基本流程
mybatis执行流程 1. 加载配置文件并初始化(SqlSession) 配置文件来源于两个地方,一个是配置文件(主配置文件conf.xml,mapper文件*.xml), 一个是java代码中 ...
- poj_3253_priority queue
描述 农夫约翰想要修篱墙,他需要N块木板,第i块板长Li.然后他买了一块很长的板子,足够他分成N块.忽略每次锯板子带来的损失. 约翰忘记买锯子了,于是像Don借.Don要收费,每次锯一下,就要收一次板 ...
- 原型模式故事链--new一个对象的过程
上一个总标题:https://segmentfault.com/a/11...提问:你有对象了吗?答:没有.笨!new一个不就好了吗! 问题点:为什么我要理解new一个对象的过程?答:不理解这个过程, ...
- 有关表单autocomplete = "off" 失效问题解决方案
一.autocomplete介绍 autocomplete是Html5中的新属性.该属性规定输入字段是否应该启用自动完成功能.自动完成允许浏览器预测对字段的输入.当用户在字段开始键入的时候,浏览器基于 ...
- SpringMVC基于注解开发的步骤
基于xml配置 .1准备好以下相关jar包 .2创建Maven项目使用骨架 (这里选择第二个以webapp结尾的非第一个) 给项目起个名字 这里可以更改maven本地仓库(依赖包所存放的地方)的路径 ...
- Js中的三个错误语句:try、catch、throw
Js中的三个错误语句:try.catch.throw
- rbac介绍、自动生成接口文档、jwt介绍与快速签发认证、jwt定制返回格式
今日内容概要 RBAC 自动生成接口文档 jwt介绍与快速使用 jwt定制返回格式 jwt源码分析 内容详细 1.RBAC(重要) # RBAC 是基于角色的访问控制(Role-Based Acces ...
- 无需debug,通过抽象模型快速梳理代码核心流程
上一篇我们通过DSM来确定了核心对象并构建了抽象模型.本篇是<如何高效阅读源码>专题的第八篇,我们来基于抽象模型来梳理核心流程. 本节主要内容: 如何通过抽象模型来梳理核心流程 从类名和注 ...