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 ...
随机推荐
- 【JS 逆向百例】网洛者反爬练习平台第四题:JSFuck 加密
关注微信公众号:K哥爬虫,持续分享爬虫进阶.JS/安卓逆向等技术干货! 声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后 ...
- 宏观上看Spring创建对象的过程
宏观上看Spring创建对象的过程 对于对象而言,可以分为简单对象和复杂对象: 简单对象 简单对象指可以直接new的对象: Spring在创建这些对象时,是基于反射来完成的. 复杂对象 复杂对象指不能 ...
- 提高Android Studio的编译速度(更快出包减少等待)
硬件和软件的准备 对于经常要出包而且一次要出多个渠道APK的同事来说,每次漫长的打包等待是一件消耗生命且无意义事情. google官方提高编译速度的文档:https://developer.andro ...
- 如何区分Unity国内版和国际版
从这三个地方都可以判断使用的Unity是国内版本还是国际版,国内版的版本号后面会多出c1,而国际版则不会有c1结尾. 安装目录名 unity hub - 安装 - 查看当前安装的Unity各版本 un ...
- 在K8S中,PV和PVC是如何关联?
在Kubernetes(简称K8s)中,PersistentVolume (PV) 和 PersistentVolumeClaim (PVC) 是实现存储持久化的关键组件.它们之间的关联是用来动态或静 ...
- JQ模糊查询插件
//构造函数写法 ;(function($,window,document,undefined){//注意这里的分号必须加 //插件的全部代码 var FazzSearch = function (e ...
- unordered_map模拟实现|STL源码剖析系列|开散列
博主很久没有更新过STL源码剖析这个系列的文章了,主要是因为大部分STL常用的容器,博主都已经发过文章了,今天博主带着大家把哈希表也模拟实现一下. 前言 那么这里博主先安利一下一些干货满满的专栏啦! ...
- 会话跟踪技术之COOKIE
会话跟踪技术之COOKIE 一.为什么要用会话控制 我们需要我们的站点可以跟踪客户端与服务器之间的交互,保存和记忆每个用户的身份和信息. 几个疑问 我先访问A页面后访问B页面,HTTP无法知道是不是同 ...
- FDConnection的事务测试讲解。。
总之用事务的宗旨是: 1.不用嵌套事务EnableNested设置为False 2.事务一定要回滚,避免发生异常的情况下,没有回滚 造成,不可估量的错误. try frmClientDm.MyMain ...
- .NET Core开发实战(第25课:路由与终结点:如何规划好你的Web API)--学习笔记(下)
25 | 路由与终结点:如何规划好你的Web API 自定义约束实现了路由约束接口,它只有一个 Match 方法,这个方法传入了 Http 当前的 httpContext,route,routeKey ...