1. map的介绍
map的介绍
- map是关联容器,它按照特定的次序(按照key来比较)存储由键值key和值value组合而成的元素。
- 在map中,键值key通常用于排序和惟一地标识元素,而值value中存储与此键值key关联的内容。键值key和值value的类型可能不同,并且在map的内部,key与value通过成员类value_type绑定在一起,为其取别名称为pair:typedef pair value_type;
- 在内部,map中的元素总是按照键值key进行比较排序的。
- map中通过键值访问单个元素的速度通常比unordered_map容器慢,但map允许根据顺序对元素进行直接迭代(即对map中的元素进行迭代时,可以得到一个有序的序列)。
- map支持下标访问符,即在[ ]中放入key,就可以找到与key对应的value。
- map通常被实现为二叉搜索树(更准确的说:平衡二叉搜索树(红黑树))。
2.map的使用
-
map的模板参数说明
key: 键值对中key的类型
T: 键值对中value的类型
Compare: 比较器的类型,map中的元素是按照key来比较的,缺省情况下按照小于来比较,一般情况下(内置类型元素)该参数不需要传递,如果无法比较时(自定义类型),需要用户自己显式传递比较规则(一般情况下按照函数指针或者仿函数来传递)
Alloc:通过空间配置器来申请底层空间,不需要用户传递,除非用户不想使用标准库提供的空间配置器
注意:在使用map时,需要包含头文件#include -
map的构造
-
map的迭代器
-
map的容量与元素访问
注意:在元素访问时,有一个与operator[]类似的操作at()(该函数不常用)函数,都是通过key找到与key对应的value然后返回其引用,不同的是:当key不存在时,operator[]用默认value与key构造键值对然后插入,返回该默认value,at()函数直接抛异常。 -
map中元素的修改
4.map的总结- map中的的元素是键值对。
- map中的key是唯一的,并且不能修改。
- 默认按照小于的方式对key进行比较。
- map中的元素如果用迭代器去遍历,可以得到一个有序的序列。
- map的底层为平衡搜索树(红黑树),查找效率比较高。
- 支持[]操作符,operator[]中实际进行插入查找。
5.实例
void test()
{
int arr[] = { 1,10,2,5,11,18,0,0,1,10,5 };
//multiset可以存放重复度数据
multiset<int> s(arr, arr + sizeof(arr) / sizeof(arr[0]));
for (auto& e : s)
{
cout << e << " ";
}
cout << endl;
//set会去重
set<int> s1(arr, arr + sizeof(arr) / sizeof(arr[0]));
for (auto& a : s1)
{
cout << a << " ";
}
cout << endl;
}
void test()
{
map<int, int> m;
//插入的类型为pair对象
//两种常见的插入方式
//1.调用pair构造函数创建对象
auto ret = m.insert(pair<int, int>(1, 1)); //插入成功
cout << ret.first->first << "-->" << ret.first->second << "insert:"<<ret.second << endl;
//2.调用make_pair函数创建对象
ret = m.insert(make_pair(2, 2)); //插入成功
cout << ret.first->first << "-->" << ret.first->second << "insert:" <<ret.second<<endl;
ret = m.insert(make_pair(2, 3)); //k值已经存在,插入失败
cout << ret.first->first << "-->" << ret.first->second << "insert:" << ret.second << endl;
//删除k为2的值
size_t num = m.erase(2);
cout << num << endl;
cout << m.erase(2) << endl; //已经被删除了
//查找按照key值查找
auto it = m.find(1);
cout << it->first << it->second << endl; // 1 1
//查找失败,返回end迭代器
it = m.find(10);
cout << (it == m.end()) << endl; //1
}
void test()
{
pair<int, int> arr[] = { pair<int,int>(5,5),pair<int,int>(1,2),
pair<int,int>(3,3),pair<int,int>(0,0),pair<int,int>(1,3)};
map<int, int> m2(arr, arr + sizeof(arr) / sizeof(arr[0]));
//访问operator[]
cout << m2[3] << endl; //3
cout << m2[1] << endl; //2
//修改
m2[1] = 100; //1-->100
//插入一个新的键值对
m2[100] = 100; //100-->0 -- 100-->100
cout << m2[100] << endl;
//at接口执行的操作,如果key不存在,抛异常
//cout << m2.at(1000) << endl;
}
template<class T1,class T2>
void printMap(const map<T1,T2>& m)
{
//map中的数据是pair
//迭代器访问的顺序:按照key的中序遍历
map<T1, T2>::const_iterator it = m.begin();
while (it != m.end())
{
//不能直接输出pair对象
//cout<<it;
cout << it->first << "-->" << it->second << endl;
++it;
}
}
template <class T1, class T2>
void printMap2(const map<T1, T2>& m)
{
//map中的数据是pair
//迭代器访问的顺序:按照key的中序遍历
map<T1, T2>::const_reverse_iterator it = m.rbegin();
while (it != m.rend())
{
//不能直接输出pair对象
//cout<<it;
cout << it->first << "-->" << it->second << endl;
++it;
}
}
void test()
{
map<int, int> m;
pair<int, int> arr[] = { pair<int,int>(5,5),pair<int,int>(1,2),
pair<int,int>(3,3),pair<int,int>(0,0),pair<int,int>(1,3)};
//map中key不能重复,value可以重复
map<int, int> m2(arr,arr+sizeof(arr)/sizeof(arr[0]));
//printMap(m2);
//printMap2(m2);
/*map<int, int, greater<int>> m3(arr, arr + sizeof(arr) / sizeof(arr[0]));
for (auto& p : m3)
{
cout << p.first << "-->" << p.second << endl;
}*/
//map的迭代器可以修改value,不能修改key
/*auto& it = m2.begin();
it->second = 1000;*/
}