C++ //set/multiset 容器 //set不可以插入重复的数字 multiset可以插入重复的数字 //ste容器构造和赋值 //set大小和交换 //set 插入和删除 //set查找和统计 //set 和 multiset 区别 //pair 对组创建 //set存放自定义数据类型 //set内置数据 进行排序
1 //set/multiset 容器 //set不可以插入重复的数字 multiset可以插入重复的数字
2 //ste容器构造和赋值 //set大小和交换 //set 插入和删除
3 //set查找和统计 //set 和 multiset 区别
4 //pair 对组创建 //set存放自定义数据类型 //set内置数据 进行排序
5
6
7 #include<iostream>
8 #include<string>
9 #include<set>
10
11 using namespace std;
12 void printSet(set<int>&s)
13 {
14 for (set<int>::iterator it = s.begin(); it != s.end(); it++)
15 {
16 cout << *it << " ";
17 }
18 cout << endl;
19 }
20 void test01()
21 {
22 set<int>s1;
23 //插入数据 只有insert方式
24 s1.insert(10);
25 s1.insert(40);
26 s1.insert(40);
27 s1.insert(30);
28 s1.insert(20);
29
30
31 //遍历容器
32 //set容器特点:所有元素插入时候会被排序
33 //set容器不允许插入重复的元素
34 printSet(s1);
35
36
37 //拷贝构造
38 set<int>s2(s1);
39 printSet(s2);
40
41 //赋值操作
42 set<int>s3;
43 s3 = s2;
44 printSet(s3);
45
46 }
47
48 //set大小和交换
49 void test02()
50 {
51 set<int>s2;
52 s2.insert(10);
53 s2.insert(20);
54 s2.insert(30);
55 s2.insert(40);
56 s2.insert(40);
57
58 printSet(s2);
59
60 //是否为空
61 if (s2.empty())
62 {
63 cout << "s2为空!" << endl;
64 }
65 else
66 {
67 cout << "s2不为空!" << endl;
68 cout << "s2的个数为:" << s2.size() << endl;
69 }
70
71 set<int>s3;
72 s3.insert(100);
73 s3.insert(200);
74 s3.insert(3000);
75 s3.insert(400);
76 s3.insert(401);
77
78 cout << "交换前:" << endl;
79 printSet(s2);
80 printSet(s3);
81 cout << "交换后:" << endl;
82
83 s2.swap(s3);
84 printSet(s2);
85 printSet(s3);
86
87
88
89
90 }
91
92
93 //set 插入和删除
94 void test03()
95 {
96 set<int>s3;
97 s3.insert(100);
98 s3.insert(200);
99 s3.insert(3000);
100 s3.insert(400);
101 s3.insert(401);
102
103 printSet(s3);
104
105 //删除
106 s3.erase(s3.begin());
107 printSet(s3);
108
109 //删除重载
110 s3.erase(200);
111 printSet(s3);
112
113 //清空
114 s3.erase(s3.begin(), s3.end());
115 printSet(s3);
116
117 s3.clear();
118 printSet(s3);
119 }
120
121 //set查找和统计
122 void test04()
123 {
124 set<int>s4;
125 s4.insert(100);
126 s4.insert(200);
127 s4.insert(3000);
128 s4.insert(400);
129 s4.insert(401);
130 printSet(s4);
131
132 set<int>::iterator pos = s4.find(20000);
133
134 if (pos != s4.end())
135 {
136 cout << "找到了元素:" << *pos << endl;
137 }
138 else
139 {
140 cout << "没有找到!" << endl;
141 }
142
143
144 }
145
146 //统计
147 void test05()
148 {
149 set<int>s4;
150 s4.insert(100);
151 s4.insert(200);
152 s4.insert(3000);
153 s4.insert(400);
154 s4.insert(401);
155 printSet(s4);
156
157 //统计
158 int num = s4.count(20000);
159 //对于set而言 统计结果 1 or 0
160 cout << "num = " << num << endl;
161
162 }
163
164 //set 和 multiset 区别
165
166 void test06()
167 {
168 set<int>s6;
169
170 pair<set<int>::iterator, bool> ret = s6.insert(10);
171
172 if (ret.second)
173 {
174 cout << "第一次插入成功!" << endl;
175 }
176 else
177 {
178 cout << "第一次插入失败!" << endl;
179 }
180 ret = s6.insert(10);
181 if (ret.second)
182 {
183 cout << "第二次插入成功!" << endl;
184 }
185 else
186 {
187 cout << "第二次插入失败!" << endl;
188 }
189
190 multiset<int>m1;
191 //允许插入重复值
192 m1.insert(10);
193 m1.insert(10);
194 m1.insert(10);
195 m1.insert(10);
196 m1.insert(10);
197 m1.insert(10);
198
199 for (multiset<int>::iterator it = m1.begin(); it != m1.end(); it++)
200 {
201 cout << *it << " ";
202 }
203 cout << endl;
204
205 }
206
207 //pair 对组创建 pair<type,type> p (value1,value2) pair<type,type>
208 //p = make_pair(value1,value2);
209 void test07()
210 {
211 //第一种
212 pair<string ,int>p("张三", 20);
213 cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
214
215 //第二种
216 pair<string, int>p2 = make_pair("lisi", 20);
217 cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
218 }
219 //set容器排序 存放内置数据类型
220 //仿函数 从大到小
221 class MyCompare
222 {
223 public:
224 bool operator()(int v1,int v2)const
225 {
226 return v1 > v2;
227 }
228 };
229 void test08()
230 {
231 set<int>s8;
232 s8.insert(20);
233 s8.insert(30);
234 s8.insert(10);
235 s8.insert(50);
236 s8.insert(40);
237
238
239 for (set<int>::iterator it = s8.begin(); it != s8.end(); it++)
240 {
241 cout << *it << " ";
242 }
243 cout << endl;
244
245 //指定排序规则 从大到小
246 set<int,MyCompare>s9;
247 s9.insert(20);
248 s9.insert(30);
249 s9.insert(10);
250 s9.insert(50);
251 s9.insert(40);
252
253 for (set<int,MyCompare>::iterator it = s9.begin(); it != s9.end(); it++)
254 {
255 cout << *it << " ";
256 }
257 cout << endl;
258 }
259
260 //set排序存放自定义数据类型
261 class Person
262 {
263 public:
264 Person(string name, int age)
265 {
266 this->m_Age = age;
267 this->m_Name = name;
268 }
269
270 string m_Name;
271 int m_Age;
272 };
273
274 //仿函数
275 class MyComparePerson
276 {
277 public:
278 bool operator()(const Person&p1,const Person&p2)const
279 {
280 //按照年龄 降序
281 return p1.m_Age > p2.m_Age;
282 }
283 };
284 void test09()
285 {
286 //自定义的数据类型 都会指定排序规则
287 set<Person, MyComparePerson>s;
288
289 //创建Person对象
290 Person p1("刘备", 25);
291 Person p2("关羽", 300);
292 Person p3("张飞", 78);
293 Person p4("曹操", 20);
294
295 s.insert(p1);
296 s.insert(p2);
297 s.insert(p3);
298 s.insert(p4);
299
300 for (set<Person, MyComparePerson>::iterator it = s.begin(); it != s.end(); it++)
301 {
302 cout << "姓名:" << it->m_Name << "\t年龄: " << it->m_Age << endl;
303 }
304
305 }
306
307
308 int main()
309 {
310 test01();
311
312
313 test02();
314 test03();
315 test04();
316 test05();
317
318 test06();
319 test07();
320
321 test08();
322
323 test09();
324 system("pause");
325 return 0;
326 }

C++ //set/multiset 容器 //set不可以插入重复的数字 multiset可以插入重复的数字 //ste容器构造和赋值 //set大小和交换 //set 插入和删除 //set查找和统计 //set 和 multiset 区别 //pair 对组创建 //set存放自定义数据类型 //set内置数据 进行排序的更多相关文章
- STL——容器(Set & multiset)的默认构造 & 带参构造 & 对象的拷贝构造与赋值
1. 默认构造 set<int> setInt; //一个存放int的set容器. set<float> setFloat; //一 ...
- STL——容器(Map & multimap)的拷贝构造与赋值
1. Map & multimap 的拷贝构造与赋值 map(const map &mp); //拷贝构造函数 map& operator=(con ...
- Python中内置数据类型list,tuple,dict,set的区别和用法
Python中内置数据类型list,tuple,dict,set的区别和用法 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, ...
- 容器间通信的三种方式 - 每天5分钟玩转 Docker 容器技术(35)
容器之间可通过 IP,Docker DNS Server 或 joined 容器三种方式通信. IP 通信 从上一节的例子可以得出这样一个结论:两个容器要能通信,必须要有属于同一个网络的网卡. 满足这 ...
- IOC容器在web容器中初始化过程——(二)深入理解Listener方式装载IOC容器方式
先来看一下ContextServletListener的代码 public class ContextLoaderListener extends ContextLoader implements S ...
- 顺序容器----顺序容器操作,vector对象如何增长,额外的string操作,容器适配器
一.顺序容器操作 1.向顺序容器添加元素 向顺序容器(array除外)添加元素的操作: 操作 说明 c.push_back(t) 在c的尾部创建一个值为t的元素.返回void c.emplace_ba ...
- AngularJS内建服务以及自定义服务的用法
在AngularJS中, 服务是一个比较重要的部分,它是一个对象或者是函数,可以在你的AngularJS的应用中使用.接下来介绍几种比较常用的内建服务以及自定义服务的方法. [内建服务] (1)loc ...
- 条目七《如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉》
如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉 在STL中容器是智能的,可以在容器销毁时自动调用容器里对象的析构函数来销毁容器存储的对象. STL的容器虽然比较智能 ...
- SQL删除语句DROP、TRUNCATE、 DELETE 的区别
主要介绍了SQL删除语句DROP.TRUNCATE. DELETE 的区别,帮助大家更好的理解和学习sql语句,感兴趣的朋友可以了解下 DROP: 1 DROP TABLE test; 删除表test ...
- mysql和oracle建表语句以及数据类型的区别
1.mysql和oracle建表语句的区别 mysql DROP TABLE IF EXISTS `order`;CREATE TABLE `order` ( `id` int(11) NOT NU ...
随机推荐
- Git的使用(二):远程仓库
在github上创建远程仓库 本地创建Git仓库适合自己一个人完成工程,但是实际情况中我们需要其他人来协作开发,此时就可以把本地仓库同步到远程仓库,同时还增加了本地仓库的一个备份.常用的远程仓库就是g ...
- 4.6 C++ Boost 函数绑定回调库
Boost 库是一个由C/C++语言的开发者创建并更新维护的开源类库,其提供了许多功能强大的程序库和工具,用于开发高质量.可移植.高效的C应用程序.Boost库可以作为标准C库的后备,通常被称为准标准 ...
- LyScript 批量搜索反汇编特征
LyScript 插件实现对特定汇编指令片段的批量搜索功能,用户传入一个汇编指令列表,然后循环搜索该列表内的所有指令特征,如果找到了,则返回该指令的内存地址. 插件地址:https://github. ...
- mysql删除表中重复记录
1.创建测试表,并插入数据 create table test( id int(11) primary key auto_increment comment '用户编号', username varc ...
- 音频处理实用AI工具
最近在做音频处理相关的工作,主要有以下几个好用的工具. 1. 语音转文字--whisper 这是一款由OpenAI开发的语音转文字工具,项目地址位于:openai/whisper. 这个工具是用来生成 ...
- DBGrideh使用小结【EditorMode】当用户点击单元格的时候,自动进入编辑状态
(1)控制一个单元格是否允许编辑(或者说文字选中),可以设置Grid的Options的dgEditing属性:如果该属性为False,那么用户只能选中该单元格而无法选中里面的内容,也无法进行编辑.(2 ...
- .NET Core开发实战(第24课:文件提供程序:让你可以将文件放在任何地方)--学习笔记
24 | 文件提供程序:让你可以将文件放在任何地方 文件提供程序核心类型: 1.IFileProvider 2.IFileInfo 3.IDirectoryContents IFileProvider ...
- Java-生成字符串的MD5值
方法一:public static String getMd5(String str) { MessageDigest md5 = null; try { md5 = MessageDigest.ge ...
- react中的setState是同步还是异步?react为什么要将其设计成异步?
壹 ❀ 引 了解react的同学都知道,react遵守渲染公式UI=Render(state),状态决定了组件UI最终渲染的样子(props也可以理解为外部传入的状态),由此可见state对于reac ...
- ES6学习 第一章 let 和 const 命令
前言: 最近开始看阮一峰老师的<ECMAScript 6 入门>(以下简称原文)学习ECMAScript 6(下文简称ES6)的知识,整理出一些知识点加上我的理解来做成文章笔记.按照章节为 ...