C++官网参考链接:https://cplusplus.com/reference/map/multimap/
类模板
<map>
std::multimap
template < class Key,// multimap::key_type
class T,// multimap::mapped_type
class Compare = less<Key>,// multimap::key_compare
class Alloc = allocator<pair<const Key,T> >// multimap::allocator_type
> class multimap;
multimap
multimap是关联容器,它存储由键值和映射值按特定顺序组合而成的元素,其中多个元素可以具有等价的键。
在multimap中,键值通常用于对元素进行排序和唯一标识,而映射值存储与此键相关的内容。键和映射值的类型可能不同,并被分组在成员类型value_type中,这是一个组合了两者的pair类型:
typedef pair<const Key, T> value_type;
在内部,multimap中的元素总是按其键排序,并遵循由其内部比较对象(comparison object)(类型Compare)指示的特定严格弱排序标准。
在按键访问单个元素时,multimap容器通常比unordered_multimap容器慢,但它们允许基于顺序对子集进行直接迭代。
multimap通常实现为二叉搜索树。
容器的属性
有关联的
关联容器中的元素是通过它们的键来引用的,而不是通过它们在容器中的绝对位置。
有顺序的
容器中的元素始终遵循严格的顺序。所有插入的元素都按此顺序给出一个位置。
映射
每个元素都将一个键与映射值关联:键用于标识其主要内容为映射值的元素。
多个等价键
容器中的多个元素可以具有等价的键。
能够感知的allocator
容器使用allocator对象动态处理其存储需求。
模板形参
Key
键的类型。映射中的每个元素都由其键值标识。
别名为成员类型multimap::key_type。
T
映射值的类型。multimap中的每个元素都存储一些数据作为其映射值。
别名为成员类型multimap::mapped_type。
Compare
一个二元谓词,接受两个元素键作为实参并返回bool值。表达式comp(a,b),其中comp是这种类型的对象,a和b是元素键,如果按照函数定义的严格弱顺序,a被认为在b之前,则返回true。
multimap对象使用这个表达式来确定元素在容器中的顺序以及两个元素键是否等价(通过条件反射式比较:if(!comp(a,b) && !comp(b,a)))。
这可以是函数指针或函数对象(参见构造函数(constructor)的示例)。它默认为less<T>,返回的结果与应用less-than操作符(a<b)相同。
别名为成员类型multimap::key_compare。
Alloc
用于定义存储分配模型的allocator对象的类型。默认情况下,使用allocator类模板,它定义了最简单的内存分配模型,并且与值无关。
别名为成员类型multimap::allocator_type。
成员类型
C++98
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 | allocator_type::reference | for the default allocator: value_type& |
const_reference | allocator_type::const_reference | for the default allocator: const value_type& |
pointer | allocator_type::pointer | for the default allocator: value_type* |
const_pointer | 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 |
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 multimap (public member function)
(destructor) Multimap destructor (public member function)
operator= Copy container content (public member function)
iterator:
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)
修改器:
insert Insert element (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)
观测器:
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)