C++进阶-3-6-map/multimap容器
C++进阶-3-6-map/multimap容器
1 #include<iostream>
2 #include<map>
3 using namespace std;
4
5 // map / multimap容器
6
7 void printMap(map<int, int>& m) {
8 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
9 cout << "key = " << (*it).first << " value = " << it->second << endl;
10 }
11 cout << endl;
12 }
13
14 // 1.构造和赋值
15 void test01() {
16
17 // 创建map容器
18 map<int, int> m;
19
20 m.insert(pair<int, int>(1, 10));
21 m.insert(pair<int, int>(2, 20));
22 m.insert(pair<int, int>(3, 30));
23 m.insert(pair<int, int>(4, 40));
24
25 printMap(m);
26
27 // 拷贝构造
28 map<int, int>m2(m);
29 printMap(m2);
30
31 // 赋值
32 map<int, int>m3;
33 m3 = m2;
34 printMap(m3);
35
36 }
37
38 // 2.大小和交换
39 void test02() {
40
41 map<int, int> m;
42
43 m.insert(pair<int, int>(1, 10));
44 m.insert(pair<int, int>(2, 20));
45 m.insert(pair<int, int>(3, 30));
46 m.insert(pair<int, int>(4, 40));
47
48 //printMap(m);
49
50 // 大小
51 if (m.empty()) {
52 cout << "m 为空" << endl;
53 }
54 else
55 {
56 cout << "m 不为空" << endl;
57 cout << "m 的大小为:" << m.size() << endl;
58 }
59
60 // 交换
61 map<int, int> m2;
62
63 m2.insert(pair<int, int>(5, 50));
64 m2.insert(pair<int, int>(6, 60));
65 m2.insert(pair<int, int>(7, 70));
66 m2.insert(pair<int, int>(8, 80));
67
68 cout << "交换前:" << endl;
69 printMap(m);
70 printMap(m2);
71
72 cout << "交换后:" << endl;
73 m.swap(m2);
74 printMap(m);
75 printMap(m2);
76 }
77
78 // 3.插入和删除
79 void test03() {
80
81 map<int, int> m;
82
83 // 插入
84 // 第一种
85 m.insert(pair<int, int>(1, 10));
86 printMap(m);
87
88 // 第二种
89 m.insert(make_pair(2, 20));
90 printMap(m);
91
92 // 第三种
93 m.insert(map<int, int>::value_type(3, 30));
94 printMap(m);
95
96 // 第四种
97 m[4] = 40; // 不建议插入使用
98 printMap(m);
99
100
101 // 删除
102 m.erase(m.begin());
103 printMap(m);
104
105 m.erase(3); // 按照key删除
106 printMap(m);
107
108 // 清空
109 //m.erase(m.begin(), m.end());
110 m.clear();
111 printMap(m);
112 }
113
114 // 4.查找和统计
115 void test04() {
116
117 map<int, int> m;
118
119 m.insert(pair<int, int>(1, 10));
120 m.insert(pair<int, int>(2, 20));
121 m.insert(pair<int, int>(3, 30));
122 m.insert(pair<int, int>(4, 40));
123
124 printMap(m);
125
126 // 查找,find返回的是迭代器
127 map<int, int>::iterator pos = m.find(3);
128
129 if (pos != m.end()) {
130 cout << "查到了元素,key = " << (*pos).first << " value = " << pos->second << endl;
131 }
132 else
133 {
134 cout << "未找到元素" << endl;
135 }
136
137 // 统计, map中无重复的key,所以,统计值为0或1
138 // multimap的count统计可能大于1
139 int num = m.count(1);
140 cout << "num = " << num << endl;
141
142 }
143
144 // 5. 排序
145
146 class MyCompare {
147 public:
148 bool operator()(int v1, int v2) {
149 // 降序
150 return v1 > v2;
151 }
152
153 };
154
155 void printMap1(map<int, int, MyCompare>& m) {
156 for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
157 cout << "key = " << (*it).first << " value = " << it->second << endl;
158 }
159 cout << endl;
160 }
161
162 void test05() {
163
164 map<int, int> m;
165
166 m.insert(pair<int, int>(1, 10));
167 m.insert(pair<int, int>(2, 20));
168 m.insert(pair<int, int>(3, 30));
169 m.insert(pair<int, int>(4, 40));
170
171 // 排序 默认:从小到大,升序
172 printMap(m);
173
174 // 降序
175 map<int, int, MyCompare> m2;
176
177 m2.insert(pair<int, int>(1, 10));
178 m2.insert(pair<int, int>(2, 20));
179 m2.insert(pair<int, int>(3, 30));
180 m2.insert(pair<int, int>(4, 40));
181
182 printMap1(m2);
183
184 }
185
186 int main() {
187
188 // 1.构造和赋值
189 //test01();
190
191 // 2.大小和交换
192 //test02();
193
194 // 3.插入和删除
195 //test03();
196
197 // 4.查找和统计
198 //test04();
199
200 // 5. 排序,默认,从小到大升序
201 test05();
202
203 system("pause");
204
205 return 0;
206 }
207
208 // 总结
209 //
210 // map / multimap容器
211 //
212 // 简介:
213 // map中所有元素都是pair
214 // pair中第一个元素为key,起索引作用,第二个元素为value
215 // 所有元素都回根据元素的键值自动排序
216 //
217 // 本质:属于关联式容器,地层结构用二叉树实现
218 //
219 // 优点:可以根据key值快速找到value值
220 //
221 // map / multimap区别:
222 // map不允许容器中有重复的key
223 // multi允许容器中有重复的key
224 //
C++进阶-3-6-map/multimap容器的更多相关文章
- 2.9 C++STL map/multimap容器详解
文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...
- iBinary C++STL模板库关联容器之map/multimap
目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...
- STL学习系列九:Map和multimap容器
1.map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...
- 09--STL关联容器(map/multimap)
一:map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...
- STL之Map和multimap容器
1.Map和multimap容器 1)map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. 2)map中key值是唯一的.集合中的元素按一 ...
- C++ STL 学习笔记__(8)map和multimap容器
10.2.9 Map和multimap容器 map/multimap的简介 ² map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. ² ...
- STL Map和multimap 容器
STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力. ...
- STL——容器(Map & multimap)的删除
Map & multimap 的删除 map.clear(); //删除所有元素 map.erase(pos); //删除pos迭代器所指的元素,返回下一个元素的 ...
- STL——容器(Map & multimap)的大小
1. Map & multimap 的大小 map.size(); //返回容器中元素的数目 map.empty();//判断容器是否为空, 容器中有内容将会返回 false 代码示例 ...
随机推荐
- Robinhood基于Apache Hudi的下一代数据湖实践
1. 摘要 Robinhood 的使命是使所有人的金融民主化. Robinhood 内部不同级别的持续数据分析和数据驱动决策是实现这一使命的基础. 我们有各种数据源--OLTP 数据库.事件流和各种第 ...
- Architecture Principles
Architecture Principles - Completed Components Name Statement Rationale Implications TOGAF Principle ...
- IPhoneX网页布局简介
IPhoneX全面屏是十分科技化的,但是由于其圆角和摄像头刘海位置以及操控黑条的存在使得我们需要去对其样式做一些适配,没有X的同学可以开启 Xcode 9 的iPhone X 模拟器作为学习和调试. ...
- [译] 沙箱中的间谍 - 可行的 JavaScript 高速缓存区攻击
原文 The Spy in the Sandbox – Practical Cache Attacks in Javascript 相关论文可在 https://github.com/wyvernno ...
- CSS系列——浏览器默认样式
了解HTML标签在各浏览器当中的默认样式,可以让我们了解,为什么会要写Reset.css,Reset.css当中要怎么写样式最合理.试着思考下面的问题: 为什么会有默认样式? 每个浏览器的默认样式有什 ...
- Sentry前端部署拓展篇(sourcemap关联、issue关联、release控制)
原文首发于我的个人博客: https://lonhon.top/ 之前的<基础篇>主要介绍了Sentry和基本部署流程,在实际使用过程中你会发现Sentry受欢迎的原因:除了单纯的监控异常 ...
- python-成绩转换
本题要求编写程序将一个百分制成绩转换为五分制成绩.转换规则: 大于等于90分为A: 小于90且大于等于80为B: 小于80且大于等于70为C: 小于70且大于等于60为D: 小于60为E. 输入样例: ...
- MySQL---char和varchar的区别
char和varchar的区别 char表示定长, 即长度固定. varchar表示变长, 即长度可变. 当输入数据的长度小于定义的长度时, char会用空格填充, 而varchar则按照实际长度存储 ...
- 邮件任务-springboot
邮件任务-springboot springboot可以很容易实现邮件的发送 具体实现步骤: 导入jar包 <dependency> <groupId>org.springfr ...
- linux压缩及解压命令
.zip文件:压缩:zip,解压:unzip 如果要解压到指定目录,可以加上 -d 选项 .gz文件:压缩:gzip,解压:gunzip 压缩.解压缩后原文件丢失,可以加上 -c 选项利用 linux ...