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容器排序的更多相关文章

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

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

  2. 6.12---知道参数的重要性------插入数据-删除数据-修改数据注意Map

    ---------------

  3. map和multimap映射容器

    map容器 map所处理的数据与数据库表具有键值的记录非常相似,在键值与映射数据之间,建立一个数学上的映射关系.map容器的数据结构仍然採用红黑树进行管理.插入的元素键值不同意反复,所使用的结点元素的 ...

  4. 【C++ STL】Map和Multimap

    1.结构 Map和multimap将key/value pair(键值/实值 队组)当作元素,进行管理.他们根据key的排序准则将元素排序.multimap允许重复元素,map不允许. 元素要求: k ...

  5. STL学习系列九:Map和multimap容器

    1.map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...

  6. STL之Map和multimap容器

    1.Map和multimap容器 1)map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. 2)map中key值是唯一的.集合中的元素按一 ...

  7. C++ STL 学习笔记__(8)map和multimap容器

    10.2.9 Map和multimap容器 map/multimap的简介 ²  map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. ² ...

  8. STL Map和multimap 容器

    STL Map和multimap 容器 map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供 基于key的快速检索能力.       ...

  9. STL学习笔记— —容器map和multimap

    简单介绍 在头文件<map> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...

  10. STL的基本使用之关联容器:map和multiMap的基本使用

    STL的基本使用之关联容器:map和multiMap的基本使用 简介 map 和 multimap 内部也都是使用红黑树来实现,他们存储的是键值对,并且会自动将元素的key进行排序.两者不同在于map ...

随机推荐

  1. 从零开始匹配vim(1)——选项设置

    前面我们算是对 vimscript 做了一个入门,并且实现了一个 输出 hello world 的语句.现在我们继续进行 vimscript 的学习. set语句 之前在介绍 vim 基础的时候,我们 ...

  2. centos7多网口配置同网段IP解决方案

    环境 CentOS Linux release 7.9.2009 (Core) 需求 服务器eth0和eth1配置同网段IP地址.掩码不配网关,同时连接两根网线,对端是两台物理隔离的交换机. 现象 给 ...

  3. Laravel日期处理

    1. 常用: echo Carbon::now(); // 2023-04-08 18:07:24 echo Carbon::today(); // 2023-04-08 00:00:00 echo ...

  4. Java-统计程序运行的时长(计算两个时间相差的毫秒数、秒数)

    最近在做Hbase的查询性能验证,需要统计查询的执行时长,所以需要统计开始时间和结束时间的时间差. 下面是使用SimpleDateFormat和Date计算时间差(相差毫秒数)的程序示例,仅供参考. ...

  5. 编译pjsip源码

    操作系统 : Windows 10_x64 [版本 10.0.19042.685] pjsip版本 : 2.10 pjsip官网:https://www.pjsip.org/ 1. 下载pjsip源代 ...

  6. UVA12390 Distributing Ballot Boxes 题解

    题目传送门 题意 有 \(n\) 个城市,\(b\) 个投票箱,第 \(i\) 个城市有 \(a_i\) 人,每个人均有一张票,将 \(b\) 个投票箱分给 \(n\) 个城市,每个城市的票分摊在投票 ...

  7. js 实现call和apply方法,超详细思路分析

    壹 ❀ 引 我在 五种绑定策略彻底弄懂this 一文中,我们提到call,apply,bind属于显示绑定,这三个方法都能直接修改this指向.其中call与apply比较特殊,它们在修改this的同 ...

  8. NEMU PA 3-1 实验报告

    一.实验目的 在前面的PA1中,我们实现了CPU和FPU,在PA2中我们实现了对指令的解码和对ELF的装载,以及进一步完善了CLI调试器.那么在整个PA3中,我们将要着力于内存的相关处理,如Cache ...

  9. ES6学习 第七章 函数的扩展

    前言 本章介绍函数的扩展.有些不常用的知识了解即可. 本章原文链接:函数的扩展. 函数参数的默认值 ES6 允许为函数的参数设置默认值,即直接写在参数定义的后面. 当函数形参没有被赋值时,才会将默认值 ...

  10. SATA学习笔记——Link Layer 加扰/解扰/CRC

    一.故事前传 我们之前说到Link layer的结构,link layer的作用大致可以包括以下几点: Frame flow control CRC的生成与检测 对数据与控制字符的Scrmable/D ...