一、map简介
1、作用
C++中的map提供了一种数据的映射查找方法,即一对一的hash。map中自建一棵红黑树并对数据进行即使排序。
实例:比如班级中学号和姓名一一对应(且学号唯一,姓名可不唯一)时,就可以存储在map中,可供快速通过学号来查找姓名。
2、头文件
#include<map>
3、声明
map <key_type, value_type> map_name
key:关键字,map中唯一,相当于实例中的学号;
value:值,map中可不唯一,相当于实例中的学生名字
示例:map <int, string> StudentMap,即建立了一个key为int类型,value为string类型,名叫StudentMap的map。以下所有示例均以StudentMap作为map所定义的实体的名称。
二、map用法
1、4种插入方法
StudentMap.insert ( pair<int, string>(1, "李明“) );
StudentMap.insert ( make_pair(1, "李明") );
StudentMap.insert ( map<int, string>::value_type(1, "李明") );
StudentMap [1] = "李明";
第一种:插入一个数据类型为<int, string>的对儿。
第二种:用make_pair,默认插入数据的数据类型同定义map实体时用的类型相同。
第三种:就是当作固定格式就成。
第四种:像数组一样直接赋值。
2、函数
1、迭代器
四种迭代器:begin(), end(), rbegin, rend()
分别是:正向迭代第一个地址,正向迭代最后一个的下一个地址,反向迭代第一个地址,反向迭代最后一个的下一个地址。“r”表示反向的意思。
注意1:map.begin()等返回的是一个地址。
注意2:插入时维持"key唯一"的原则,当插入的key值已有时,此次插入无效。(有返回值反馈)
Q: 为什么反向的时候不直接用end到begin,而是还需要用rbegin到rend呢?
A: 因为end并非指向最后一个元素,而是最后一个的下一个元素。
实例:
for (auto i=StudentMap.begin(); i!=StudentMap.end(); i++) {
cout << "key=" << i->first << ", value=" << i->second << endl;
}
/*
1. StudentMap.end()指向的是map中最后一对数据的下一个位置
2. i->first是键key,i->second是值value
3. 由于i是地址,*i是地址所指向的实体,所以"i->first"也可以写成"(*i).first"
4. auto表示自动获取数据类型,且只有c++11以上才可支持。
*/
2、通过迭代器删除
iter = StudentMap.find("李明");
StudentMap.erase(iter);
3、通过关键字删除
int value = StudentMap.erase("李明");
成功删除返回1,删除失败返回0。
4、用迭代器进行范围删除
StudentMap.erase( StudentMap.begin(), StudentMap.end() );
相当于StudentMap.clear();
5、清空/判空
清空:map.clear();判空:map.empty()
清空:将map容器中的所有数据对全部删除
判空:为空返回1,还有数据返回0
6、查找
addr = StudentMap.find( "李明" );
if ( addr != StudentMap.end() )
cout << "value=" << addr->second << endl;
else
cout<< "not find" << endl;
如查找成功:find()返回其地址。
如查找失败:find()返回map.end(),注意map.end()时最后一个数据的下一个位置的地址。
7、map大小
int size = StudentMap.size();
int max_size = StudentMap.max_size();
当前容量和最大容量(通常上亿)
8、返回对应key的个数
int c = StudentMap.count();
由于map中key的值只有0或1两种可能,所以返回值也只有0/1。
9、返回对应value的“地址(也可以说迭代器)”
iter = StudentMap.lower_bound(x),返回值为x的数据的地址。
iter = StudentMap.upper_bound(x),返回值为x的数据的下一个地址。
Pair = StudentMap.equal_range(x);
返回值是一个pair类型,pair< map<char, int>::iterator, map<char, int>::iterator > Pair。其中Pair.first指的是StudentMap.lower_bound(x);Pair.second指的是StudentMap.upper_bound(x)。
实例:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
int main(){
pair< map<char, int>::iterator, map<char, int>::iterator > res;
// 这个pair是两个地址组成的<lower_bound, upper_bound>
map<char, int> StudentMap;
StudentMap.insert(make_pair('a', 1));
StudentMap.insert(make_pair('d', 10));
StudentMap.insert(make_pair('b', 4));
StudentMap.insert(make_pair('c', 4));
// 此时虽然value非顺序输入,但map自动排序
res = StudentMap.equal_range('f');
cout << "lower: key=" << res.first->first;
cout << ", value=" << res.first->second << endl;
cout << "upper: key=" << res.second->first;
cout << ", value=" << res.second->second << endl;
return 0;
}
/*
outoput:
lower: key=c, value=4
upper: key=d, value=10
*/
10、get_allocator
待完成
11、swap
map<int, int> map_1;
map<int, int> map_2;
map_2.swap (map_1); 即可交换map_1和map_2的内容。
12、key_comp
待完成










