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 ...
随机推荐
- slices in Go 1.21
Go 1.21中新增的 slices包中提供了很多与切片相关的函数,适用于任意类型的切片. 本文内容来自官方文档 BinarySearch 函数签名如下: func BinarySearch[S ~[ ...
- 微信小程序-页面跳转wxAPI
官方文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateTo.html wx.navigateTo(O ...
- AiTrust下预训练和小样本学习在中文医疗信息处理挑战榜CBLUE表现
项目链接: https://aistudio.baidu.com/aistudio/projectdetail/4592515?contributionType=1 如果有图片缺失参考项目链接 0.项 ...
- paddleNLP-BUG和一些细节记录【一】
1.TypeError: isfile() takes 1 positional argument but 2 were given File "/root/miniconda3/envs/ ...
- tensorflow语法【tf.matmul() 、loc和iloc函数、tf.expand_dims()】
相关文章: [一]tensorflow安装.常用python镜像源.tensorflow 深度学习强化学习教学 [二]tensorflow调试报错.tensorflow 深度学习强化学习教学 [三]t ...
- Centos安装Python3.8
最直白的centos8安装python3.8yum install -y update安装 gcc和make插件:yum install gcc gcc-c++yum -y install gcc a ...
- IDEA破解激活
!!!不要使用最新2021.2.3以后的版本,没有30天免费试用.推荐使用2021年之前的版本!!! 1: IDEA安装后使用30天免费试用进入,然后找到图中位置点击 2: 点击下图链接下载破解jar ...
- git常用命令(企业级)
一: 常用git命令 # 初始化,将已有的文件初始化为git仓库 git init # 查询文件状态[绿色暂存区,红色表示工作区更改了,没有提交到暂存区] git status git status ...
- 案例:OGG目标端进程ABENDED处理
源端环境:RHEL 6.5 + Oracle 11.2.0.4 RAC + OGG 19.1.0.0.4 目标端环境:RHEL 7.6 + Oracle 19.3 + OGG 19.1.0.0.4 故 ...
- Python内置小工具(非常实用!)
一.1秒钟启动一个下载服务器在工作中时不时会有这样的一个需求:将服务器(或者自己电脑)上的文件传给其他同事.将文件传给同事本身并不是一个很繁琐的工作,现在的聊天工具一般都支持文件传输.但是,如果需要传 ...