模板:
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通过指定的顺序,来存储键值和元素值结合的元素。
在map中,键值经常被用来排序,而且每一个键值在map中是独一无二的。键值和映射值的类型可能不同,并被分组在成员类型value_type中,这是一个组合两者的pair类型:
typedef pair<const Key, T> value_type;
在内部,映射中的元素总是根据其键按照内部比较对象(类型为Compare)所指示的特定严格的弱排序条件进行排序。
map容器通常比unordered_map容器通过它们的键访问单个元素的速度慢,但是它们允许根据它们的顺序对子集进行直接迭代。
映射中的值可以通过对应的键使用括号操作符((operator[])直接访问。
map通常被实现为二叉搜索树。
属性:
关联 | 关联容器中的元素由它们的键引用,而不是由它们在容器中的绝对位置引用。 |
排序 | 容器中的元素始终遵循严格的顺序。所有插入的元素都按这个顺序给定一个位置。 |
Map | 每个元素都将一个键关联到一个映射值:键表示标识其主要内容为映射值的元素。 |
Key | 容器中的任何两个元素都不能具有相同的键。 |
Allocator-aware | 容器使用一个分配器对象来动态地处理它的存储需求。 |
模板参数:
Key | 键的类型。映射中的每个元素都由其键值唯一标识。 别名为成员类型映射::key_type。 |
T | 映射值的类型。映射中的每个元素都将一些数据存储为其映射值。 别名为成员类型映射::mapped_type。 |
Compare | 以两个元素键作为参数并返回一个bool值。表达式compare(a,b)中,compare是这种类型的对象,a和b是键值,如果在函数定义的严格弱序中,a被认为在b之前,则表达式comp(a,b)应该返回true。 map对象使用这个表达式来确定元素在容器中的顺序以及两个元素键是否相等(通过反射性地比较它们:如果 !compare(a,b) && !compare(b,a),它们是相等的)。map容器中的任何两个元素都不能具有相同的键。 它可以是函数指针,也可以是函数对象(参见构造函数)。这默认为less<T>,它返回的结果与应用小于操作符(a<b)相同。 别名为成员类型映射::key_compare。 |
Alloc | 用于定义存储分配模型的分配器对象的类型。默认情况下,使用的是分配器类模板,它定义了最简单的内存分配模型,并且是与值无关的。 别名为成员类型映射::allocator_type。 |
成员类型:
C++11:
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) | Construct map (public member function ) |
(destructor) | Construct map (public member function ) |
operator= | Copy container content (public member function ) |
迭代器:
begin | Return iterator to beginning (public member function ) |
end | Return iterator to end (public member function ) |
rbegin | Return reverse iterator to reverse beginning (public member function ) |
rend | Return reverse iterator to reverse end (public member function ) |
cbegin | Return const_iterator to beginning (public member function ) |
cend | Return const_iterator to end (public member function ) |
crbegin | Return const_reverse_iterator to reverse beginning (public member function ) |
crend | Return const_reverse_iterator to reverse end (public member function ) |
容量:
empty | Test whether container is empty (public member function ) |
size | Return container size (public member function ) |
max_size | Return maximum size (public member function ) |
获取元素:
operator[] | Access element (public member function ) |
at | Access element (public member function ) |
修改:
insert | Insert elements (public member function ) |
erase | Erase elements (public member function ) |
swap | Swap content (public member function ) |
clear | Clear content (public member function ) |
emplace | Construct and insert element (public member function ) |
emplace_hint | Construct and insert element with hint (public member function ) |
Observers:
key_comp | Return key comparison object (public member function ) |
value_comp | Return value comparison object (public member function ) |
操作:
find | Get iterator to element (public member function ) |
count | Count elements with a specific key (public member function ) |
lower_bound | Return iterator to lower bound (public member function ) |
upper_bound | Return iterator to upper bound (public member function ) |
equal_range | Get range of equal elements (public member function ) |
Allocator:
get_allocator | Get allocator (public member function ) |