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内置数据 进行排序的更多相关文章

  1. STL——容器(Set & multiset)的默认构造 & 带参构造 & 对象的拷贝构造与赋值

    1. 默认构造 set<int> setInt;              //一个存放int的set容器. set<float> setFloat;          //一 ...

  2. STL——容器(Map & multimap)的拷贝构造与赋值

    1. Map & multimap 的拷贝构造与赋值 map(const map &mp);               //拷贝构造函数 map& operator=(con ...

  3. Python中内置数据类型list,tuple,dict,set的区别和用法

    Python中内置数据类型list,tuple,dict,set的区别和用法 Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, ...

  4. 容器间通信的三种方式 - 每天5分钟玩转 Docker 容器技术(35)

    容器之间可通过 IP,Docker DNS Server 或 joined 容器三种方式通信. IP 通信 从上一节的例子可以得出这样一个结论:两个容器要能通信,必须要有属于同一个网络的网卡. 满足这 ...

  5. IOC容器在web容器中初始化过程——(二)深入理解Listener方式装载IOC容器方式

    先来看一下ContextServletListener的代码 public class ContextLoaderListener extends ContextLoader implements S ...

  6. 顺序容器----顺序容器操作,vector对象如何增长,额外的string操作,容器适配器

    一.顺序容器操作 1.向顺序容器添加元素 向顺序容器(array除外)添加元素的操作: 操作 说明 c.push_back(t) 在c的尾部创建一个值为t的元素.返回void c.emplace_ba ...

  7. AngularJS内建服务以及自定义服务的用法

    在AngularJS中, 服务是一个比较重要的部分,它是一个对象或者是函数,可以在你的AngularJS的应用中使用.接下来介绍几种比较常用的内建服务以及自定义服务的方法. [内建服务] (1)loc ...

  8. 条目七《如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉》

    如果容器中包含了通过new操作创建的指针,切记在容器对象析构前将指针delete掉 在STL中容器是智能的,可以在容器销毁时自动调用容器里对象的析构函数来销毁容器存储的对象. STL的容器虽然比较智能 ...

  9. SQL删除语句DROP、TRUNCATE、 DELETE 的区别

    主要介绍了SQL删除语句DROP.TRUNCATE. DELETE 的区别,帮助大家更好的理解和学习sql语句,感兴趣的朋友可以了解下 DROP: 1 DROP TABLE test; 删除表test ...

  10. mysql和oracle建表语句以及数据类型的区别

    1.mysql和oracle建表语句的区别 mysql DROP TABLE IF EXISTS `order`;CREATE TABLE `order` (  `id` int(11) NOT NU ...

随机推荐

  1. ansible使用,搭建mongo的replica-set小结

    ansible 前言 常用到的指令 查看ip是否可用 执行 执行,查看日志输出 查看这个 playbook 的执行会影响到哪些 hosts 设置服务器免密登录 ansible了解 变量名的使用 pla ...

  2. Python 提取图片中的GPS信息

    JPG图片中默认存在敏感数据,例如位置,相机类型等,可以使用Python脚本提取出来,加以利用,自己手动拍摄一张照片,然后就能解析出这些敏感数据了,对于渗透测试信息搜索有一定帮助,但有些相机默认会抹除 ...

  3. 同时配置github和gitee秘钥

    1.设置用户名和邮箱 git config --global --list 查看全局配置信息 git config --global --list 删除配置:必须删除该设置 git config -- ...

  4. linux(centos) 下搭建svn服务器

     1. 使用yum安装svn yum -y install subversion 安装完成之后,验证安装结果 此命令会全自动安装svn服务器相关服务和依赖,安装完成会自动停止命令运行 若需查看svn安 ...

  5. WebAssembly核心编程[4]: Memory

    由于Memory存储的是单纯的二进制字节,所以原则上我们可以用来它作为媒介,在wasm模块和数组程序之间传递任何类型的数据.在JavaScript API中,Memory通过WebAssembly.M ...

  6. strobe

    总是喜欢一个人出神,置身的场景经常是小时有趣的明晃晃的下午.也不知道为什么印象中有趣的下午的阳光总是让人睁不开眼,我也曾试图给大脑传递过"能不能将那晃眼的阳光删去",但再次置身仍是 ...

  7. AdoQuery 多列 查询 定位方法

    frmClientDm.TopItemSkuShow_adoq.Locate('top_outer_iid;top_outer_sid', VarArrayOf([top_outer_iid,top_ ...

  8. 欢迎加入 DotNet NB 交流学习群

    目录 起因 创建群组 群成员 技术交流 社区推广 社区前辈 欢迎加入 起因 自从2019年参加 .NET Conf China 大会之后,我创办了一个公众号 DotNet NB,内容主要是 关于 .N ...

  9. 到什么程度才叫精通 Linux?

    大家好,我是陶朱公Boy,一个认真生活,总想超越自己的程序员. 前言 知乎上有一个提问:到什么程度才叫精通 Linux?                              ↓↓↓ 今天,我们就 ...

  10. wxPython 笔记

    安装 Win7 / Win10 直接通过 pip install wxpython 安装 Ubuntu18.04 / Ubuntu 20.04 在Linux下的安装会稍微麻烦, 可以参考官网上的说明  ...