C++ map //map/multimap容器 //map容器 构造和赋值 //map大小 和 交换 //map插入和删除 //map查找和统计 //map容器排序
1 //map/multimap容器 //map容器 构造和赋值 //map大小 和 交换
2 //map插入和删除 //map查找和统计 //map容器排序
3
4 #include<iostream>
5 #include<map>
6 #include<string>
7
8 using namespace std;
9
10 //map容器 构造和赋值
11 //打印
12 void printMap(map<int, int>& m)
13 {
14 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
15 {
16 cout << "Key = " << (*it).first << " value= " << it->second << endl;
17
18 }
19 cout << endl;
20 }
21 //map容器 构造和赋值
22 void test01()
23 {
24 map<int, int>m;
25 m.insert(pair<int,int>(1,10));
26 m.insert(pair<int,int>(2,20));
27 m.insert(pair<int,int>(3,30));
28 m.insert(pair<int,int>(4,40));
29
30 printMap(m);
31
32 //拷贝构造
33 map<int, int>m2(m);
34 printMap(m2);
35 //赋值
36 map<int, int>m3;
37 m3 = m2;
38 printMap(m3);
39
40
41 }
42 //map大小 和 交换
43 //大小
44 void test02()
45 {
46 map<int, int>m;
47 m.insert(pair<int, int>(1, 10));
48 //m.insert(pair<int, int>(1, 10));
49 m.insert(pair<int, int>(2, 20));
50 m.insert(pair<int, int>(3, 30));
51 m.insert(pair<int, int>(4, 40));
52
53 if (m.empty())
54 {
55 cout << "M为空!!" << endl;
56 }
57 else
58 {
59 cout << "M不为空!!" << endl;
60 cout << "M的元素个数!" << m.size() << endl;
61 }
62
63 }
64 //交换
65 void test03()
66 {
67 map<int, int>m1;
68 m1.insert(pair<int, int>(1, 10));
69 //m.insert(pair<int, int>(1, 10));
70 m1.insert(pair<int, int>(2, 20));
71 m1.insert(pair<int, int>(3, 30));
72 m1.insert(pair<int, int>(4, 40));
73
74 map<int, int>m2;
75 m2.insert(pair<int, int>(5, 100));
76 //m.insert(pair<int, int>(1, 100));
77 m2.insert(pair<int, int>(6, 200));
78 m2.insert(pair<int, int>(7, 300));
79 m2.insert(pair<int, int>(8, 400));
80
81 cout << "交换前:" << endl;
82 printMap(m1);
83 printMap(m2);
84
85 cout << "交换后:" << endl;
86 m1.swap(m2);
87 printMap(m1);
88 printMap(m2);
89
90
91 }
92 //打印
93 void printMap4(map<int, int>& m)
94 {
95 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
96 {
97 cout << "key =" << it->first<<" value ="<<it->second<<endl;
98 }
99 cout << endl;
100 }
101
102 //map插入和删除
103 void test04()
104 {
105 map<int, int>m;
106 //插入
107 //第一种
108 m.insert(pair<int, int>(1, 10));
109
110 //第二种
111 m.insert(make_pair(2, 40));
112
113 //第三种
114 m.insert(map<int, int>::value_type(3, 30));
115
116 //第四种
117 m[4] = 40;
118
119 //[]不建议取插入 可以去key访问到value
120 cout << m[4] << endl;
121 printMap4(m);
122
123 //删除
124 m.erase(m.begin());
125 printMap4(m);
126
127 m.erase(3); //按照KRY 删除
128 printMap4(m);
129
130 //清空
131 m.erase(m.begin(), m.end());
132 printMap4(m);
133 //清空
134 m.clear();
135 printMap4(m);
136 }
137
138 //map查找和统计
139 void test05()
140 {
141 //查找
142 map<int, int>m;
143 m.insert(make_pair(1, 10));
144 m.insert(make_pair(2, 20));
145 m.insert(pair<int, int>(3, 30));
146 m.insert(pair<int, int>(4, 40));
147 m[4] = 20; //map不允许插入重复的key的值
148 map<int,int>::iterator pos =m.find(3);
149 if (pos != m.end())
150 {
151 cout << "查到了元素 key =" << (*pos).first << "\tvalue=" << pos->second << endl;
152 }
153 else
154 {
155 cout << "没有找到!!" << endl;
156 }
157 //统计
158 //统计要么是0 要么是1 .... multimap统计可以大于1
159 int num =m.count(3);
160 cout << "num = " << num << endl;
161
162
163
164 }
165 //排序
166 class MyCompare
167 {
168 public:
169 bool operator()( int v1, int v2) const
170 {
171 return v1 > v2;
172 }
173 };
174
175 //map容器排序.
176 void test06()
177 {
178 map<int, int>m1;
179 m1.insert(pair<int, int>(1, 20));
180 m1.insert(pair<int, int>(2, 30));
181 m1.insert(make_pair(3, 40));
182 m1.insert(make_pair(4, 50));
183 m1.insert(make_pair(5, 10));
184 cout << "排序前:" << endl;
185 for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++)
186 {
187
188 cout << "key =" << it->first << "\tvalue =" << (*it).second << endl;
189
190 }
191
192 map<int, int, MyCompare>m;
193 m.insert(pair<int, int>(1, 20));
194 m.insert(pair<int, int>(2, 30));
195 m.insert(make_pair(3,40));
196 m.insert(make_pair(4, 50));
197 m.insert(make_pair(5, 10));
198
199 cout << "排序后:" << endl;
200 for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
201 {
202 cout << "key =" << it->first << "\tvalue =" <<(*it).second<< endl;
203
204 }
205 }
206
207 int main()
208 {
209
210 test01();
211 test02();
212 test03();
213 test04();
214 test05();
215 test06();
216 system("pause");
217 return 0;
218 }
C++ map //map/multimap容器 //map容器 构造和赋值 //map大小 和 交换 //map插入和删除 //map查找和统计 //map容器排序的更多相关文章
- STL——容器(Map & multimap)的拷贝构造与赋值
1. Map & multimap 的拷贝构造与赋值 map(const map &mp); //拷贝构造函数 map& operator=(con ...
- 6.12---知道参数的重要性------插入数据-删除数据-修改数据注意Map
---------------
- map和multimap映射容器
map容器 map所处理的数据与数据库表具有键值的记录非常相似,在键值与映射数据之间,建立一个数学上的映射关系.map容器的数据结构仍然採用红黑树进行管理.插入的元素键值不同意反复,所使用的结点元素的 ...
- 【C++ STL】Map和Multimap
1.结构 Map和multimap将key/value pair(键值/实值 队组)当作元素,进行管理.他们根据key的排序准则将元素排序.multimap允许重复元素,map不允许. 元素要求: k ...
- STL学习系列九:Map和multimap容器
1.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> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...
- STL的基本使用之关联容器:map和multiMap的基本使用
STL的基本使用之关联容器:map和multiMap的基本使用 简介 map 和 multimap 内部也都是使用红黑树来实现,他们存储的是键值对,并且会自动将元素的key进行排序.两者不同在于map ...
随机推荐
- OpenIM Open Source Instant Messaging Project Docker Compose Deployment Guide
The deployment of OpenIM involves multiple components and supports various methods including source ...
- TeXStudio与Bakoma TeX 结合实现实时阅览
参考链接:VSCode 或 TeXStudio LaTeX 配置方法 - 知乎 相信大家在使用TeXStudio时候,每次修改完毕都要运行一下再能看到PDF界面,这样做十分不方便,因此先给出如下操作办 ...
- 操作系统开发:BIOS/MBR基础与调试
这里在实验之前需要下载 Bochs-win32-2.6.11 作者使用的是Linux版本的,在Linux写代码不太舒服,所以最好在Windows上做实验,下载好虚拟机以后还需要下载Nasm汇编器,以及 ...
- OSW Analyzer分析oswbb日志发生异常
具体OSW Analyzer详细介绍可以参考MOS文档: OSWatcher Analyzer User Guide (Doc ID 461053.1) 我们常用的就是拿到一份osw数据到自己电脑,使 ...
- Python 爬虫方法总结
实现爬虫的套路 准备URL 准备start_url url地址规律不明显,总数不确定 通过代码提取下一页的url 通过xpath提取 寻找url地址,部分参数在当前的响应中(比如当前页码数和总页码数在 ...
- 反悔贪心&模拟费用流
贪心是一种常用的算法,它能够获得局部最优解,但我们往往需要的是全局最优解,所以我们在贪心的时候加入和反悔的机制,让他能够得到全局最优解. 由于网络流中的退流操作本质上也是反悔贪心,所以在实现反悔贪心时 ...
- JS Leetcode 1370. 上升下降字符串 题解分析,桶排序与charCodeAt fromCharCode妙用
壹 ❀ 引 本题来自LeetCode1370. 上升下降字符串,难度简单,是一道考察对于字符串遍历熟练度的题目,题目描述如下: 给你一个字符串 s ,请你根据下面的算法重新构造字符串: 从 s 中选出 ...
- Java设计模式-模板模式Template
介绍 模板方法模式(Template Method Pattern),又叫模板模式(Template Pattern),z 在一个抽象类公开定义了执行.它的方法的模板.它的子类可以按需要重写方法实现, ...
- 图文并茂之AES加密
本文改编自:http://www.sohu.com/a/198681357_505794 假设有一个发送方在向接收方发送消息.如果没有任何加密算法,接收方发送的是一个明文消息:"我是小灰&q ...
- base::AtExitManager 和 base::Singleton 的结合使用
单例模式(Singleton)也称为单件模式,其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.有很多地方需要这样的功能模块,如系统的日志输出,GUI 应用必须是 ...