时间:2021-04-28来源:www.pcxitongcheng.com作者:电脑系统城
map 表示映射,可以将任何基本类型(包括 STL 容器)映射到任何基本类型(包括 STL 容器),例如可以建立如 int 到 double,string 到 int 的映射等。
map 提供一对一的 hash,该功能类似 Python 的字典:
<bits/stdc++.h> 头文件已经包括了该头文件。
定义 map 如下,参数的第一个为 key 的类型,第二个为 value 的类型。
| 1 | map<typename1, typename2> mp; |
【注意】如果是字符串到整型的映射,必须使用 string 而不能用 char 数组。
map 的键和值也可以是 STL 容器,例如可以将一个 set 容器映射到一个字符串:
| 1 | map<set<int>, string> mp; |
注意:map 的键是唯一的。
| 1 2 3 4 5 6 7 8 9 |
#include <iostream>#include <map>using namespace std;int main(){ map<char, int> mp; mp['c'] = 30; cout << mp['c'] << endl; return 0;} |
30
定义迭代器:
| 1 | map<typename1, typename2>::iterator it; |
这样可以得到迭代器 it,map 可以使用 it->first来访问键,使用 it->second 来访问值。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h>#include <map>using namespace std;int main(){ map<char, int> mp; mp['m'] = 20; mp['r'] = 30; mp['a'] = 40; for(map<char, int>::iterator it = mp.begin(); it!=mp.end();it++){ printf("%c %d\n", it->first, it->second); } return 0;} |
输出:
a 40
m 20
r 30
【注意】map 会以键从小到大的顺序自动排序。迭代器的比较不能用 < 或者 >,而只能使用 == 或者 !=
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h>#include <map>using namespace std;int main(){ map<char, int> mp; mp['m'] = 20; mp['r'] = 30; mp['a'] = 40; for(map<char, int>::reverse_iterator it = mp.rbegin(); it!=mp.rend();it++){ printf("%c %d\n", it->first, it->second); } return 0;} |
输出:
r 30
m 20
a 40
rbegin()指向 map 的最后一个元素,rend()指向 map 第一个元素之前。
(1)通过insert + <key, value> 插入
| 1 2 |
map<int, string> mapStudent; mapStudent.insert(pair<int, string>(1, "student_one")); |
(2)通过insert + 迭代器 插入
| 1 2 |
map<int, string> mapStudent; mapStudent.insert(map<int, string>::value_type (1, "student_one")); |
(3)通过数组方式插入
| 1 2 |
map<int, string> mapStudent; mapStudent[1] = "student_one"; |
【注意】第一、二种方法完全等价,但是第三种和前两种有所区别。当映射中包含了键,则第一、二中方法插入失败,而第三种方法会覆盖之前的键值对。所以先后插入相同 key 的元素,第一、二种方法会保留第一次的数据,第三种会保留最后一次的。
find(key) 返回键为 key 的映射的迭代器,时间复杂度为 O(logN),N为 map 中映射的个数。
| 1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h>#include <map>using namespace std;int main(){ map<char, int> mp; mp['a'] = 1; mp['b'] = 2; mp['c'] = 3; map<char, int>::iterator it = mp.find('b'); printf("%c %d\n", it->first, it->second); return 0;} |
b 2
① 删除单个元素
mp.erase(it) :it 是要删除的元素的迭代器,时间复杂度为 O(1)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h>#include <map>using namespace std;int main(){ map<char, int> mp; mp['a'] = 1; mp['b'] = 2; mp['c'] = 3; map<char, int>::iterator it = mp.find('b'); mp.erase(it); // 删除 b 2 for(map<char, int>::iterator it = mp.begin(); it!=mp.end();it++){ printf("%c %d\n", it->first, it->second); } return 0;} |
a 1
c 3
mp.erase(key):key是要删除的映射的键,时间复杂度为 O(logN)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h>#include <map>using namespace std;int main(){ map<char, int> mp; mp['a'] = 1; mp['b'] = 2; mp['c'] = 3; mp.erase('b'); // 删除 b 2 for(map<char, int>::iterator it = mp.begin(); it!=mp.end();it++){ printf("%c %d\n", it->first, it->second); } return 0;} |
a 1
c 3
② 删除一个区间内所有元素
mp.erase(first, last):first 为需要删除区间的起始迭代器,last 为需要删除的区间的末尾迭代器的下一个地址,即删除左闭右开区间 [first, last) 内所有元素。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h>#include <map>using namespace std;int main(){ map<char, int> mp; mp['a'] = 1; mp['b'] = 2; mp['c'] = 3; map<char, int>::iterator it = mp.find('b'); // 令it指向键为b的映射 mp.erase(it, mp.end()); // 删除it之后所有的映射 for(map<char, int>::iterator it = mp.begin(); it!=mp.end();it++){ printf("%c %d\n", it->first, it->second); } return 0;} |
a 1
size() :获取 map 中映射的对数,时间复杂度为 O(1)。
| 1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h>#include <map>using namespace std;int main(){ map<char, int> mp; mp['a'] = 10; mp['b'] = 20; mp['c'] = 30; printf("%d\n", mp.size()); // 3对映射 return 0;} |
count(): 返回 map 中对应键的个数,由于 map 中相同键只能最多有一个,所以 count() 的结果只能是 0 或者 1。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream>#include <map>int main (){ std::map<char,int> mymap; char c; mymap ['a']=101; mymap ['c']=202; mymap ['d']=303; for (c='a'; c<'e'; c++){ std::cout << c; if (mymap.count(c)>0) std::cout << " is an element of mymap.\n"; else std::cout << " is not an element of mymap.\n"; } return 0;} |
结果:
a is an element of mymap.
b is not an element of mymap.
c is an element of mymap.
d is an element of mymap.
clear(): 用于清空 map,map变为初始的空状态。
empty():判断 map 是否为空,如果 map 为空,返回 true,否则返回 false.
lower_bound() : 返回键值 >= 给定元素的第一个位置。即如果键的类型可以比较,可以使用二分查找的方法,返回的类型是一个迭代器。 upper_bound(): 返回键值>给定元素的第一个位置。即如果键的类型可以比较,可以使用二分查找的方法,返回的类型是一个迭代器。
| 1 2 3 4 5 6 7 |
map<int, string> mapStudent; mapStudent[1] = "student_one"; mapStudent[3] = "student_three"; mapStudent[5] = "student_five"; map<int, string>::iterator iter;iter = mapStudent.lower_bound(2); // 返回键值为3的迭代器;iter = mapStudent.upper_bound(2); // 返回键值为3的迭代器 |
以上就是c++ 数据结构map的使用详解的详细内容,
2024-07-07
Java框架如何实现非阻塞式编程?2023-03-11
Android Jetpack 组件LiveData源码解析2023-03-11
hbuilderx设置Firefox浏览器安装路径教程 hbuilderx怎么设置Firefox浏览器安装路径?一、AVL树的概念 二、AVL树节点的定义 三、AVL树的插入 四、AVL树的旋转 1.左单旋 2.右单旋 3.左右双旋 4.右左双旋 五、进行验证 六、AVLTree的性能...
2023-03-09