一、map简介
1、 什么是Map
Map是STL的一个关联容器,翻译为映射,数组也是一种映射。如:int a[10] 是int 到 int的映射,而a[5]=25,是把5映射到25。数组总是将int类型映射到其他类型。这带来一个问题,有时候希望把string映射成一个int ,数组就不方便了,这时就可以使用map。map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器)。
map提供关键字到值的映射 ,其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个称为该关键字的值,由于这个特性.
普通 int 数组是 map<int ,int > a。字符到整型的映射,就是 map<char ,int > a,而字符串到整型的映射,就必须是 map<string , int > a。map的键和值也可以是STL容器,如 map< set<int> ,string> a,而且键和值都是唯一的。
map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
map的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。
2、map的功能
- 自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
- 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
- 快速插入Key -Value 记录。
- 快速删除记录
- 根据Key 修改value记录。
- 遍历所有记录。
3、map的定义
map对象是模板类,需要关键字和存储对象两个模板参数:
map<key_type, value_type>变量名
例如:map<int,string> personnel;
这样就定义了一个用int作为索引,并拥有相关联的指向string的指针。
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
node[123456] = "张三";
cout<<"身份证号123456的人叫"<<node[123456]<<endl;
}
输出为:
身份证号123456的人叫张三
二、 map的基本操作函数:
begin() 返回指向map头部的迭代器
end() 返回指向map末尾的迭代器
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
lower_bound() 返回键值>=给定元素的第一个位置
upper_bound() 返回键值>给定元素的第一个位置
empty() 如果map为空则返回true
max_size() 返回可以容纳的最大元素个数
size() 返回map中元素的个数
clear() 删除所有元素
count() 返回指定元素出现的次数
equal_range() 返回一个pair对象(包含2个双向迭代器),其中pair.first和lower_bound()方法的返回值等价,pair.second和upper_bound()方法的返回值等价。也就是说,该方法将返回一个范围,该范围中包含的键为key的键值对(map容器键值对唯一,因此该返回最多包含一个键值对)。
erase() 删除一个元素
swap() 交换两个map
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
value_comp() 返回比较元素value的函数
三、map容器函数详解
1、 map的构造函数
map共提供了6个构造函数,这块涉及到内存分配器这些东西,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map:
map<string , int >mapstring;
map<int ,string >mapint;
map<sring, char>mapstring;
map< char ,string>mapchar;
map<char ,int>mapchar;
map<int ,char >mapint;
2、 增加数据
方法1:以数组下标的形式直接增加,即:变量名[key] = value 的形式。
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定义变量
node[123456] = "张三";
node[123457] = "李四";
node[123458] = "王五";
cout<<"身份证号123456的人叫"<<node[123456]<<endl;
cout<<"身份证号123457的人叫"<<node[123457]<<endl;
cout<<"身份证号123458的人叫"<<node[123458]<<endl;
}
输出为:
身份证号123456的人叫张三
身份证号123457的人叫李四
身份证号123458的人叫王五
方法2:直接插入键值对。
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
node.insert(pair<int, string>(123456, "张三"));
node.insert(pair<int, string>(123457, "张三"));
node.insert(pair<int, string>(123458, "李四"));
cout<<"身份证号123456的人叫"<<node[123456]<<endl;
cout<<"身份证号123457的人叫"<<node[123457]<<endl;
cout<<"身份证号123458的人叫"<<node[123458]<<endl;
}
输出为:
身份证号123456的人叫张三
身份证号123457的人叫张三
身份证号123458的人叫李四
其中,pair 定义了一个键值对,对应 map 的 key 和 value。
3、删除数据
删除数据使用到 map 的 erase 和 clear方法,来看一下例子:
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
node[123456] = "张三";
node[123457] = "李四";
node[123458] = "王五";
cout<<"size = "<<node.size()<<endl;
//1. 使用 key 删除
node.erase(123456); // 删除 key = 123456 的节点
cout<<"size = "<<node.size()<<endl;
//2. 使用迭代器删除
map<int,string>::iterator iter = node.find(123457);
node.erase(iter);
cout<<"size = "<<node.size()<<endl;
//3. 清空整个容器
node.clear();
cout<<"size = "<<node.size()<<endl;
}
输出为:
size = 3
size = 2
size = 1
size = 0
其中,clear 方法表示清空容器,size 方法表示获取容器大小。
4、 修改数据
修改数据仅能修改 value 的值,key 是不能修改的,可以通过增加和删除来实现修改 key。
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
node[123456] = "张三";
cout<<"身份证号123456的人叫"<<node[123456]<<endl;
node[123456] = "李四";
cout<<"身份证号123456的人叫"<<node[123456]<<endl;
}
输出为:
身份证号123456的人叫张三
身份证号123456的人叫李四
5、 查找数据
查找数据通过 find 函数来实现,如下所示:
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
node[123456] = "张三";
node[123457] = "李四";
node[123458] = "王五";
map<int, string>::iterator iter = node.find(123456);
if(iter != node.end())
{
cout<<"身份证号123456的人叫"<<iter->second<<endl;
}
}
输出为:
身份证号123456的人叫张三
6、遍历元素
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
node[123456] = "张三";
node[123457] = "李四";
node[123458] = "王五";
map<int, string>::iterator iter; //定义迭代器 iter
for(iter = node.begin(); iter != node.end(); ++iter)
{
cout<<"身份证号"<<iter->first<<"的人叫"<<iter->second<<endl;
}
}
输出为:
身份证号123456的人叫张三
身份证号123457的人叫李四
身份证号123458的人叫王五
其中,使用迭代器 iter 遍历容器,可以将迭代器理解为一个存储了 key 和 value 的一个结构,first 对应 key,second 对应 value。
7、交换函数 swap()
交换两个 map 容器的内容,map 容器的类型必须相同,例如:
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node1; // 定义变量
map<int, string>node2;
node1[11] = "张三";
node1[12] = "李四";
node2[21] = "王五";
node2[22] = "赵六";
node2[23] = "孙七";
node1.swap(node2);
map<int, string>::iterator iter;
cout<<"node1 :"<<endl;
for(iter = node1.begin(); iter != node1.end(); ++iter)
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
cout<<"node2 :"<<endl;
for(iter = node2.begin(); iter != node2.end(); ++iter)
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
}
输出为:
node1 :
key = 21 value = 王五
key = 22 value = 赵六
key = 23 value = 孙七
node2 :
key = 11 value = 张三
key = 12 value = 李四
8、最大元素个数max_size()
返回当前容器的可以容纳的最大元素个数,来看一个例子。
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{ map<int, string>node; // 定义变量
cout<<"max_size = "<<node.max_size()<<endl;
node[11] = "张三";
cout<<"max_size = "<<node.max_size()<<endl;
node[12] = "李四";
cout<<"max_size = "<<node.max_size()<<endl;
node[13] = "王五";
cout<<"max_size = "<<node.max_size()<<endl;
}
输出为:
max_size = 128102389400760775
max_size = 128102389400760775
max_size = 128102389400760775
max_size = 128102389400760775
9、rbegin 和 rend
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
node[11] = "张三";
node[12] = "李四";
node[13] = "王五";
map<int, string>::reverse_iterator iter;
for(iter = node.rbegin(); iter != node.rend(); ++iter)
{
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
}
}
输出为:
key = 13 value = 王五
key = 12 value = 李四
key = 11 value = 张三
注意:迭代器需要使用反向迭代器。
10、lower_bound 和 upper_bound
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main() {
map<int, string>node; // 定义变量
node[20] = "张三";
node[15] = "李四";
node[12] = "王五";
map<int, string>::iterator iter = node.lower_bound(14);
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
iter = node.upper_bound(12);
cout<<"key = "<<iter->first<<" value = "<<iter->second<<endl;
}
输出结果为:
key = 15 value = 李四
key = 15 value = 李四
11、equal_range
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
node[12] = "张三";
node[15] = "李四";
node[20] = "王五";
pair<map<int, string>::iterator, map<int, string>::iterator> p = node.equal_range(15);
cout<<"key1 = "<<p.first->first<<" value1 = "<<p.first->second<<endl;
cout<<"key2 = "<<p.second->first<<" value2 = "<<p.second->second<<endl;
}
输出为:
key1 = 15 value1 = 李四
key2 = 20 value2 = 王五
12、size()
size()用于统计当前容器的大小,也就是容器中键值对的个数。
map<string,int> myMap{{"penny",1},{"leonard",2},{"sheldon",3},{"howard",4}};
cout<<myMap.size()<<endl; //4
13、empty()
empty()用于判断当前map容器是否为空。.
map<string,int> myMap{{"penny",1},{"leonard",2},{"sheldon",3},{"howard",4}};
cout<<myMap.empty()<<endl; //0
14、count()
count(key),统计键的值为key的元素的个数,由于map中没有重复的元素,因此其计算结果只有0和1,而multimap会有多个值。
map<string,int> myMap{{"penny",1},{"leonard",2},{"sheldon",3},{"howard",4}};
int num = myMap.count("penny");
cout<<num<<endl; //1
四、排序
1. 默认排序规格
默认情况下,map容器调用less<T>规则,根据容器内各键值对的键的大小,对所有的键值进行升序排序。
因此,下面两行代码是等价的:
map<string, int> myMap{{"penny",1},{"leonard",2}};
map<string, int, less<string>> myMap{{"penny",1},{"leonard",2}};
若想要元素降序排序,则:
map<string, int, greater<string>> myMap{{"leonard",2},{"penny",1}};
例如:
map<int, string> numAndPosMap
{
{ 1, "aaa" },
{ 3, "bbb" },
{ 5, "ccc" },
{ 2, "ddd" },
{ 4, "eee" },
};
顺序为
/*
1 aaa
2 ddd
3 bbb
4 eee
5 ccc
*/
map<int, string, less<int> > numAndPosMap
{
{ 1, "aaa" },
{ 3, "bbb" },
{ 5, "ccc" },
{ 2, "ddd" },
{ 4, "eee" },
};
顺序为
/*
1 aaa
2 ddd
3 bbb
4 eee
5 ccc
*/
map<int, string , greater<int> > numAndPosMap
{
{ 1, "aaa" },
{ 3, "bbb" },
{ 5, "ccc" },
{ 2, "ddd" },
{ 4, "eee" },
};
顺序为
/*
5 ccc
4 eee
3 bbb
2 ddd
1 aaa
*/
2. 自定义排序
当key为自定义数据时,默认的排序规则就会失效,需要重写针对自定义数据的排序规则。
五、总结
编程中经常使用到 key / value 的形式表示数据之间的关系,故 map 是 STL 中经常使用的一个容器,需要记住 map 的常用方法。
还要说明的是,map中由于它内部有序,由红黑树保证,因此很多函数执行的时间复杂度都是log2N的,如果用map函数可以实现的功能,而STL Algorithm也可以完成该功能,建议用map自带函数,效率高一些。
下面说下,map在空间上的特性,否则,估计你用起来会有时候表现的比较郁闷,由于map的每个数据对应红黑树上的一个节点,这个节点在不保存你的 数据时,是占用16个字节的,一个父节点指针,左右孩子指针,还有一个枚举值(标示红黑的,相当于平衡二叉树中的平衡因子),我想大家应该知道,这些地方 很费内存。
六、map应用实例
1、P3613 【深基15.例2】寄包柜
2、HDU1263(3、http://acm.hdu.edu.cn/showproblem.php?pid=1263)
3、P3405 [USACO16DEC] Cities and States S
4、P1201 [USACO1.1] 贪婪的送礼者Greedy Gift Givers
5、CF977B Two-gram
6、P11227 [CSP-J 2024] 扑克牌