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. bootstrap ----- bootstrap table表格参数

    表格参数: 名称 标签 类型 默认 描述 - data-toggle String 'table' 不用写 JavaScript 直接启用表格. classes data-classes String ...

  2. 离线下载和安装UWP(windows应用商店)软件

    离线下载uwp安装包 打开商店,然后搜索您要的应用程序名称,进入应用界面 点击 分享按钮,在弹出窗口中选择[复制链接] 把链接粘贴到:https://store.rg-adguard.net/ 默认选 ...

  3. 基于.NET三维控件的个性化管道软件开发

    1 简介 管道广泛用于化工.工厂.建筑.市政等方面,关系到国计民生.虽然管道设计软件种类繁多,有的也非常强大(然而也非常昂贵),但也并不能完全满足个性化需要. 如何快速开发一款满足自己需求的三维管道设 ...

  4. DevToys(开发工具) v1.0.2.1

    从事开发工作的朋友们千万不要错过了!今天为大家带来的这款软件可以说是开发人员的必备工具,它就是DevToys软件!DevToys中包含了许多强大实用的开发工具,能够帮助用户将程序开发变得更加简单大大降 ...

  5. 《ASP.NET Core 与 RESTful API 开发实战》-- (第7章)-- 读书笔记(上)

    第 7 章 高级主题 7.1 缓存 缓存是一种通过存储资源的备份,在请求时返回资源备份的技术.ASP.NET Core 支持多种形式的缓存,既支持基于 HTTP 的缓存,也支持内存缓存和分布式缓存,还 ...

  6. HBase-compact介绍(minor和major区别)

    一.minor和major的区别: Minor Compaction:指选取一些小的.相邻的HFile将他们合并成一个更大的HFile,但不会清理过期(TTL)和删除(打上Delete标记)的数据.  ...

  7. NC20667 数学题

    题目链接 题目 题目描述 最近,华东交通大学ACM训练基地的老阿姨被一个数学问题困扰了很久,她希望你能够帮她解决这个问题. 这个数学问题是这样的,给你一个N,要求你计算 gcd(a,b)表示a和b的最 ...

  8. NC20573 [SDOI2011]染色

    题目链接 题目 题目描述 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如 ...

  9. NC16611 [NOIP2009]最优贸易

    题目链接 题目 题目描述 C国有n个大城市和m条道路,每条道路连接这n个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这m条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向 ...

  10. webgl 系列

    webgl 背景 工作所需... 目录 初识 WebGL 绘制一个点 三角形 变换矩阵和动画 渐变三角形 绘制猫 着色器语言