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\)在英文中的 ...
随机推荐
- docker-compose安装和使用
安装:https://my.oschina.net/thinwonton/blog/2985886 docker-compose和Dockerfile结合使用,创建django项目和postgres数 ...
- Effective Java —— 优先考虑依赖注入来引用资源
本文参考 本篇文章参考自<Effective Java>第三版第五条"Prefer dependency injection to hardwiring resources&qu ...
- 【每日日报】第五十一天---jsp
1 开始学习JSP的使用 一个简单的jsp代码 <html> <head><title>Hello World</title></head> ...
- 解决IDEA中控制台输出乱码
1. 修改VM Options(2种方法) 第一种,直接修改Tomcat中的 VM Options,这种只对当前项目有效 (1)先点击 Run -> Edit Configurations- 2 ...
- 【合集】Python基础知识【第二版】
更新部分 为了避免冗长的代码影响大家观感,将部分案例拆开 增加部分知识点,为了减少大家阅读的负担,尽可能使用短句子,但知识点不可能全覆盖,笔者不是写书,就算是写书也不可能全面,请同学们自行去补充 增加 ...
- Struts2-值栈的定义
1.值栈的概念:在Struts2里面提供本身的一种储存机制,类似于域对象,可以存值和取值(可以类比Servlet中的域对象->request,session,application) 2.具体操 ...
- C++STL 中的数值算法(iota、accumulate、adjacent_difference、inner_product、partial_sum)
以下算法均包含在头文件 numeric 中 ##1.iota 该函数可以把一个范围内的序列从给定的初始值开始累加 先看用法. 例: 假设我需要一个长度为10,从5开始递增的序列 vector<i ...
- Mysql学习day1
安装了Mysql以及SQLyog,将SQLyog和数据库做了连接. 学习了基础数据类型以及命令行语句 1 alter table `student` rename as `stu``lesson` 2 ...
- React 日常记录
以下是学习重点 原文地址 浏览器环境 JS解析和执行.绘制.事件处理.静态资源加载和处理 GUI渲染线程和Javascript线程 调度策略 先到先得(FCFS) 对短进程不利 对I/O密集不利 单处 ...
- Fastjson JdbcRowSetImpl利用链学习
JdbcRowSetImpl 接着继续学习fastjson的第二条链JdbcRowSetImpl,主要是利用jndi注入达到的攻击,而且没有什么利用限制,而且其原理就是setter的自动调用,具体se ...