STL关联式容器之map&multimap
1.map
1.1 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。>6. map通常被实现为二叉搜索树(更准确的说:平衡二叉搜索树(红黑树))
1.2 map的使用
1.2.1 map的模板参数说明
template <class Key, //键对值中Key的类型
class T, //键值对中value的类型
class Compare=less<Key> //Compare: 比较器的类型
class Alloc=allocator<pair<const Key,T>>
>class map;
注意:
1.map中的元素是按照key值来比较排序的,缺省情况下按照小于来比较,一般情况下(内置类型元素)该参数不需要传递,如果无法比较时(自定义类型),需要用户自己显式传递比较规则(一般情况下按照函数指针或者仿函数来传递)。
2.Alloc:通过空间配置器来申请底层空间,不需要用户传递,除非用户不想使用标准库提供的空间配置器。
3.使用map时需要头文件#include<map>
1.2.2 map的构造
函数声明 | 功能介绍 |
---|---|
map(); | 构造一个空的map |
map (InputIterator first, InputIterator last); | 用[first, last)区间中的元素构造map |
map (const map& x); | map的拷贝构造 |
#include<iostream>
#include<string>
#include<map>
using namespace std;
void printMap(map<string, int>& m)
{
for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "Key=" << (*it).first << " " << "Value=" << (*it).second << endl;
}
cout << endl;
}
void printMap(map<int, int>& m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "Key=" << (*it).first << " " << "Value=" << (*it).second << endl;
}
cout << endl;
}
int main()
{
// key和value的类型 int&int
map<int, int> m1;
m1.insert(pair<int, int>(1, 40));//key值 1 value值 40
m1.insert(pair<int, int>(4, 40));
m1.insert(pair<int, int>(3, 30));
m1.insert(pair<int, int>(3, 10));
// C++11 的类表初始化
map<string, int> m2{
{ "apple", 3 },
{ "banan", 6 },
{ "orange", 4 },
{ "peach", 9 },
{ "peach", 8 },
};
map<string, int> m3(m2);
printMap(m1);
printMap(m2);
cout << "m2[apple]=" << m2["apple"] << endl;
cout << "m2[peach]=" << m2["peach"] << endl;
return 0;
}
从运行结果我们可以看到map是有序的,而且key键唯一。
1.2.3 map的迭代器
函数声明 | 功能介绍 |
---|---|
begin () | 返回第一个元素的位置 |
end () | 返回最后一个元素的下一个位置 |
begin () const | 返回第一个元素的const迭代器 |
end () const | 返回最后一个元素下一个位置的const迭代器 |
rbegin () | 返回第一个元素位置的反向迭代器即rend |
rend() | 返回最后一个元素下一个位置的反向迭代器即rbegin |
rbegin() const | 返回第一个元素位置的const反向迭代器即rend |
rend() const | 返回最后一元素下一个位置的反向迭代器即rbegin |
#include<iostream>
#include<string>
#include<map>
using namespace std;
void printMap(map<string, int>& m)
{
for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "Key=" << (*it).first << " " << "Value=" << (*it).second << endl;
}
cout << endl;
}
int main()
{
map<string, int> m2{
{ "apple", 3 },
{ "banan", 6 },
{ "orange", 4 },
{ "peach", 9 },
{ "peach", 8 },
};
map<string, int> m3(m2);
//printMap(m1);
printMap(m2);
map<string, int>::iterator it = m2.begin();
cout << (*it).first << " " << (*it).second << endl;
map<string, int>::const_iterator cit = m2.cbegin();
cout << (*cit).first << " " << (*cit).second << endl;
map<string, int>::reverse_iterator rit = m2.rbegin();
cout << (*rit).first << " " << (*rit).second << endl;
map<string, int>::const_reverse_iterator crit = m2.crbegin();
cout << (*crit).first << " " << (*crit).second << endl;
return 0;
}
1.2.4 map的容量及元素访问
函数声明 | 功能介绍 |
---|---|
bool empty ( ) const | 检测map中的元素是否为空,是返回true,否则返回false |
size_type size() const | 返回map中有效元素的个数 |
mapped_type& operator[] (const key_type& k) | 返回去key对应的value |
#include<iostream>
#include<string>
#include<map>
using namespace std;
void printMap(map<string, int>& m)
{
for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "Key=" << (*it).first << " " << "Value=" << (*it).second << endl;
}
cout << endl;
}
int main()
{
map<string, int> m2{
{ "apple", 3 },
{ "banan", 6 },
{ "orange", 4 },
{ "peach", 9 },
{ "peach", 8 },
};
printMap(m2);
cout << "map中的有效元素个数:" << m2.size() << endl;
if (!m2.empty()) {
cout << "m2不为空!" << endl;
}
cout << "m2[apple]=" << m2["apple"] << endl;
cout << "m2[peach]=" << m2["peach"] << endl;
return 0;
}
问题:当key不在map中时,通过operator获取对应value时会发生什么问题?
map::operator[]文档
operator[]的原理是: 用<key, T()>构造一个键对值,然后调用insert()函数将该键值对插入到map中,如果key已经存在,插入失败,insert函数返回该key所在位置的迭代器,如果key不存在,插入成功,insert函数返回新插入元素所在位置的迭代器。operator[]函数最后将insert返回键对值中的value返回。
#include<iostream>
#include<map>
#include<string>
using namespace std;
void testmap()
{
//构造一个空的map basket
map<string, string> basket;
//将<"apple", "">插入map中,key=apple不存在,插入成功,返回value的引用
//将“苹果”赋值给该引用结果,即修改与"apple"对应的"value"为"苹果"
basket["apple"] = "苹果";
cout << basket["apple"] << endl;
cout << basket.size() << endl;
// key=banan不在map中,该函数抛异常
basket.at("banan");
}
int main()
{
testmap();
return 0;
}
在元素访问时,有一个与operator[]类似的操作at()(该函数不常用)函数,都是通过key找到与key对应的value然后返回其引用,不同的是:当key不存在时,operator[]用默认value与key构造键值对然后插入,返回该默认value,at()函数直接抛异常。
1.2.5 map中元素的修改
函数声明 | 功能介绍 |
---|---|
pair<iterator,bool> insert ( const value_type& x ) | 在map中插入键值对x,注意x是一个键值对,返回值也是键值对:iterator代表新插入元素的位置,bool代表释放插入成功;如果插入成功,返回<该元素在map中的位置,true>,如果插入失败,说明x在map中已经存在,返回<x在map中的位置,false> |
iterator insert ( iterator position, const value_type& x ) | 在position位置插入值为x的键值对,返回该键值对在map中的位置,注意:元素不一定必须插在position位置,该位置只是一个参考 |
template void insert ( InputIterator first, InputIterator last ) | 在map中插入[first, last)区间中的元素 |
void erase ( iterator position ) | 删除position位置上的元素 |
size_type erase ( const key_type& x ) | 删除键值为x的元素 |
void erase ( iterator first, iterator last ) | 删除[first,last)区间中的元素 |
void swap ( map<Key,T,Compare,Allocator>& mp ) | 交换两个map中的元素 |
void clear ( ) | 将map中的元素清空 |
iterator find ( const key_type& x) | 在map中插入key为x的元素,找到返回该元素位置的迭代器,否则返回end |
const_iterator find ( const key_type& x ) const | 在map中插入key为x的元素,找到返回该元素的位置的const迭代器,否则返回cend |
size_type count ( const key_type& x ) const | 返回key为x的键值在map中的个数,注意map中key是唯一的,因此该函数的返回值要么为0,要么为1,因此也可以用该函数来检测一个key是否在map中 |
#include<iostream>
#include<map>
#include<string>
using namespace std;
void testmap()
{
map<string, string> basket;
basket.insert(pair<string, string>("peach", "桃子"));//用键对值向map中插入元素
basket.insert(make_pair("orange", "橘子"));
basket["apple"] = "苹果";
for (auto& e : basket)
cout << e.first << "--->" << e.second << endl;
cout << endl;
auto ret = basket.insert(make_pair("peach", "桃花")); //map中的键对值key是唯一的 如果已经存在则插入失败!
if (ret.second)
cout << "<peach, 桃花>不在map中, 已经插入" << endl;
else
cout << "键值为peach的元素已经存在:" << ret.first->first << "--->" << ret.first->second << " 插入失败!" << endl;
cout << endl;
basket.erase("orange");//删除key=orange中的元素
for (auto& e : basket)
cout << e.first << "--->" << e.second << endl;
if (basket.count("orange") == 1)
cout << "orange还在" << endl;
else
cout << "orange被吃掉了" << endl;
}
int main()
{
testmap();
return 0;
}
总结:
- map中的的元素是键值对
- map中的key是唯一的,并且不能修改
- 默认按照小于的方式对key进行比较
- map中的元素如果用迭代器去遍历,可以得到一个有序的序列
- map的底层为平衡搜索树(红黑树),查找效率比较高
- 支持[]操作符,operator[]中实际进行插入查找
2.multimap
2.1multimap的介绍
- Multimaps是关联式容器,它按照特定的顺序,存储由key和value映射成的键值对<key, value>,其中多个键值对之间的key是可以重复的。
- 在multimap中,通常按照key排序和惟一地标识元素,而映射的value存储与key关联的内容。key和value的类型可能不同,通过multimap内部的成员类型value_type组合在一起,value_type是组合key和value的键值对:typedef pair<const Key, T> value_type;
- 在内部,multimap中的元素总是通过其内部比较对象,按照指定的特定严格弱排序标准对key进行排序的。
- multimap通过key访问单个元素的速度通常比unordered_multimap容器慢,但是使用迭代器直接遍历multimap中的元素可以得到关于key有序的序列。
- multimap在底层用二叉搜索树(红黑树)来实现。
multimap和map的唯一不同就是:map中的key是唯一的,而multimap中key是可以重复的。
2.2multimap的使用
- multimap中的key是可以重复的。
- multimap中的元素默认将key按照小于来比较
- multimap中没有重载operator[]操作
#include<iostream>
#include<map>
#include<string>
using namespace std;
void testmap2()
{
multimap<string, int> namelist;
namelist.insert(make_pair("孙悟空", 59));
namelist.insert(make_pair("猪八戒", 60));
namelist.insert(make_pair("沙僧", 56));
namelist.insert(make_pair("唐僧", 45));
//插入key值重复的元素
namelist.insert(make_pair("孙悟空", 61));
cout <<"namelist.size()=" <<namelist.size() << endl;
for (auto& e : namelist)
cout << e.first << "," << e.second << endl;
//计数
cout <<"count(孙悟空)="<<namelist.count("孙悟空") << endl;
}
int main()
{
testmap2();
return 0;
}