注:std::map C++11标准
map概述
template < class Key, // map::key_type class T, // map::mapped_type class Compare = less<Key>, // map::key_compare class Alloc = allocator<pair<const Key,T> > // map::allocator_type > class map;
Map是一种关联容器,它按照特定顺序存储由键值Key和映射值Value组合而成的元素。
在map中,键值Key通常用于排序和唯一标识元素,而映射值Value存储与此键值Key相关联的内容。键Key和映射值Value的类型可能不同,并在成员类型value_type中组合在一起,value_type是一个组合了这两种类型的pair类型:typedef pair<const Key, T> value_type。
在map内部,元素总是按照其键Key进行排序的,排序时使用其内部比较对象(Compare)并遵循指定的严格弱序准则。
Map容器通常比unordered_map容器在按键Key访问元素的速度上要慢,但它们允许根据子集的顺序直接迭代子集。
Map中的映射值可以通过对应的键Key使用括号操作符(operator[])直接访问。
Map通常是基于二叉搜索树实现的。
容器属性
- 关联
关联容器中的元素由其键引用,而不是由它们在容器中的绝对位置引用。
- 有序
容器中的元素始终遵循严格的顺序。所有插入的元素都按这个顺序给定一个位置。
- 映射
每个元素都将一个键关联到一个映射值:键用于标识其内容为映射值的元素。
- 键值唯一
容器中没有两个元素可以具有等效键。
- 分配器
容器使用allocator对象来动态处理其存储需求。
模板参数
- Key
键的类型。Map中的每个元素都由其键值唯一标识。别名为成员类型map::key_type。
- T
映射值的类型。Map中的每个元素都将一些数据存储为其映射值。别名为成员类型map::mapped_type
- Compare
一个二进制谓词,它接受两个键值作为参数并返回一个bool值。表达式 comp(a, b) 中,comp是Compare类型的对象,a和b是键值,如果在函数定义的严格弱序中,a被认为在b之前,则表达式comp(a, b)应该返回true。Map对象使用这个表达式来确定容器中元素的顺序以及两个元素的键是否相等。Map容器中的任何两个元素都不能具有相同的键。Compare可以是函数指针或函数对象,默认为 less<T>,其返回与使用小于操作符(a<b)相同的结果。别名为成员类型map::key_compare。
- Alloc
分配器对象的类型,用于定义存储分配模型。默认情况下,使用allocator类模板,该模板定义最简单的内存分配模型,并且与值无关。别名为成员类型map::allocator_type。
成员类型
| member type | definition | notes |
|---|---|---|
| key_type | The first template parameter (Key) | |
| mapped_type | The second template parameter (T) | |
| value_type | pair<const key_type,mapped_type> | |
| key_compare | The third template parameter (Compare) | defaults to: less<key_type> |
| value_compare | Nested function class to compare elements | see value_comp |
| allocator_type | The fourth template parameter (Alloc) | defaults to: allocator<value_type> |
| reference | value_type& | |
| const_reference | const value_type& | |
| pointer | allocator_traits<allocator_type>::pointer | for the default allocator: value_type* |
| const_pointer | allocator_traits<allocator_type>::const_pointer | for the default allocator: const value_type* |
| iterator | a bidirectional iterator to value_type | convertible to const_iterator |
| const_iterator | a bidirectional iterator to const value_type | |
| reverse_iterator | reverse_iterator<iterator> | |
| const_reverse_iterator | reverse_iterator<const_iterator> | |
| difference_type | a signed integral type, identical to: iterator_traits<iterator>::difference_type |
usually the same as ptrdiff_t |
| size_type | an unsigned integral type that can represent any non-negative value of difference_type | usually the same as size_t |
成员函数
- 构造函数(constructor)
| empty (1) |
explicit map (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc);
|
|---|---|
| range (2) |
template <class InputIterator>
map (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& = allocator_type());
|
| copy (3) |
map (const map& x); map (const map& x, const allocator_type& alloc); |
| move (4) |
map (map&& x); map (map&& x, const allocator_type& alloc); |
| initializer list (5) |
map (initializer_list<value_type> il,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
|
构造函数示例:
// constructing maps #include <iostream> #include <map> bool fncomp (char lhs, char rhs) {return lhs<rhs;} struct classcomp { bool operator() (const char& lhs, const char& rhs) const {return lhs<rhs;} }; int main () { std::map<char,int> first; first['a']=10; first['b']=30; first['c']=50; first['d']=70; std::map<char,int> second (first.begin(),first.end()); std::map<char,int> third (second); std::map<char,int,classcomp> fourth; // class as Compare bool(*fn_pt)(char,char) = fncomp; std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare return 0; }