定义

template <class _Kty, 
          class _Ty, 
          class _Pr = less<_Kty>, 
          class _Alloc = allocator<pair<const _Kty, _Ty>>>
class map

成员类型

key_type

using key_type = _Kty;

键类型。

mapped_type

using mapped_type = _Ty;

映射类型。

key_compare

using key_compare = _Pr;

键比较器。

value_compare

using value_compare = typename _Mybase::value_compare;

值比较器。

value_type

using value_type = pair<const _Kty, _Ty>;

键值类型。

allocator_type

using allocator_type = typename _Mybase::allocator_type;

内存分配器类型。

size_type

using size_type = typename _Mybase::size_type;

长度类型。

difference_type

using difference_type = typename _Mybase::difference_type;

差异类型。

pointer

using pointer = typename _Mybase::pointer;

指针类型。

const_pointer

using const_pointer = typename _Mybase::const_pointer;

常量指针类型。

reference

using reference = value_type&;

引用类型。

const_reference

using const_reference = const value_type&;

常量引用类型。

iterator

using iterator = typename _Mybase::iterator;

迭代器类型。

const_iterator

using const_iterator = typename _Mybase::const_iterator;

常量迭代器类型。

reverse_iterator

using reverse_iterator = typename _Mybase::reverse_iterator;

反向迭代器类型。

const_reverse_iterator

using const_reverse_iterator = typename _Mybase::const_reverse_iterator;

常量反向迭代器。

成员函数

构造函数

map()

构造空的 map。

explicit map(const allocator_type& _Al)

使用指定内存分配器构造 map。

map(const map& _Right)

拷贝另一个 map 构造 map。

map(const map& _Right, const allocator_type& _Al)

拷贝另一个 map,并使用指定内存分配器构造 map。

explicit map(const key_compare& _Pred)

使用指定键比较器构造 map。

map(const key_compare& _Pred, const allocator_type& _Al)

使用指定的键比较器和内存分配器构造 map。

map(_Iter _First, _Iter _Last)

使用一个迭代器区间内的元素构造 map。

map(_Iter _First, _Iter _Last, const key_compare& _Pred)

使用一个迭代器区间内的元素和指定的键比较器构造 map。

map(_Iter _First, _Iter _Last, const allocator_type& _Al)

使用一个迭代器区间内的元素和指定的内存分配器构造 map。

map(_Iter _First, _Iter _Last, const key_compare& _Pred, const allocator_type& _Al)

使用一个迭代器区间内的元素、指定的键比较器以及内存分配器构造 map。

map(map&& _Right)

使用另一个右值 map 构造 map。

map(map&& _Right, const allocator_type& _Al)

使用另一个右值 map 和指定内存分配器构造 map。

map(initializer_list<value_type> _Ilist)

用列表初始化 map。

map(initializer_list<value_type> _Ilist, const key_compare& _Pred)

以列表和键比较器初始化 map。

map(initializer_list<value_type> _Ilist, const allocator_type& _Al)

以列表和内存分配器初始化 map。

map(initializer_list<value_type> _Ilist, const key_compare& _Pred, const allocator_type& _Al)

以列表、键比较器和内存分配器初始化 map。

swap

void swap(map& _Right) noexcept(noexcept(_Mybase::swap(_Right)))

与另一个 map 交换。

insert

pair<iterator, bool> insert(_Valty&& _Val)

插入元素到 map 中。

iterator insert(const_iterator _Where, _Valty&& _Val)

插入元素到尽可能接近指定位置的地方。

try_emplace

pair<iterator, bool> try_emplace(const key_type& _Keyval, _Mappedty&&... _Mapval)

尝试构造并插入元素,如果键已存在则取消。

iterator try_emplace(const const_iterator _Hint, const key_type& _Keyval, _Mappedty&&... _Mapval)

在尽可能靠近指定位置的地方尝试构造并插入元素,如果键已存在则取消。

pair<iterator, bool> try_emplace(key_type&& _Keyval, _Mappedty&&... _Mapval)

尝试构造并插入元素,如果键已存在则取消。(右值引用)

iterator try_emplace(const const_iterator _Hint, key_type&& _Keyval, _Mappedty&&... _Mapval)

在尽可能靠近指定位置的地方尝试构造并插入元素,如果键已存在则取消。(右值引用)

insert_or_assign

pair<iterator, bool> insert_or_assign(const key_type& _Keyval, _Mappedty&& _Mapval)

插入元素,如果键已存在则覆盖值。

iterator insert_or_assign(const const_iterator _Hint, const key_type& _Keyval, _Mappedty&& _Mapval)

插入元素在尽量靠近指定位置的地方,如果键已存在则覆盖值。

pair<iterator, bool> insert_or_assign(key_type&& _Keyval, _Mappedty&& _Mapval)

插入元素,如果键已存在则覆盖值。(右值引用)

iterator insert_or_assign(const const_iterator _Hint, key_type&& _Keyval, _Mappedty&& _Mapval)

插入元素在尽量靠近指定位置的地方,如果键已存在则覆盖值。(右值引用)

at

mapped_type& at(const key_type& _Keyval)

返回键对应值的引用,没找到则抛异常。

const mapped_type& at(const key_type& _Keyval) const

返回键对应值的常量引用,没找到则抛异常。

count

size_type count(const key_type& _Keyval) const

返回对应键的数量。

size_type count(const _Other& _Keyval) const

返回与参数匹配的键值的数量。

lower_bound

iterator lower_bound(const key_type& _Keyval)

返回第一个大于等于键的对应迭代器。

const_iterator lower_bound(const key_type& _Keyval) const

返回第一个大于等于键的对应常量迭代器。

iterator lower_bound(const _Other& _Keyval)

返回第一个大于等于给定参数的键的迭代器。

const_iterator lower_bound(const _Other& _Keyval) const

返回第一个大于等于给定参数的键的常量迭代器。

upper_bound

iterator upper_bound(const key_type& _Keyval)

返回第一个大于键的对应迭代器。

const_iterator upper_bound(const key_type& _Keyval) const

返回第一个大于键的对应常量迭代器。

iterator upper_bound(const _Other& _Keyval)

返回第一个大于给定参数的键的迭代器。

const_iterator upper_bound(const _Other& _Keyval) const

返回第一个大于给定参数的键的常量迭代器。

equal_range

pair<iterator, iterator> equal_range(const key_type& _Keyval)

返回匹配对应键的迭代器范围。

pair<const_iterator, const_iterator> equal_range(const key_type& _Keyval) const

返回匹配对应键的常量迭代器范围。

pair<iterator, iterator> equal_range(const _Other& _Keyval)

返回与参数匹配的键的迭代器范围。

pair<const_iterator, const_iterator> equal_range(const _Other& _Keyval) const

返回与参数匹配的键的常量迭代器范围。

clear

void clear() noexcept

清空 map。

erase

iterator erase(iterator _Where) noexcept

删除迭代器位置的元素。

iterator erase(const_iterator _Where) noexcept

删除常量迭代器位置的元素。

iterator erase(const_iterator _First, const_iterator _Last) noexcept

删除指定常量迭代器范围内的元素。

size_type erase(const key_type& _Keyval) noexcept(noexcept(_Eqrange(_Keyval)))

删除所有匹配键的元素。

find

iterator find(const key_type& _Keyval)

返回第一个匹配键的元素的迭代器。

const_iterator find(const key_type& _Keyval) const

返回第一个匹配键的元素的常量迭代器。

iterator find(const _Other& _Keyval)

返回与参数匹配的键的第一个元素的迭代器。

const_iterator find(const _Other& _Keyval) const

返回与参数匹配的键的第一个元素的常量迭代器。

begin

iterator begin() noexcept

返回第一个元素的迭代器。

const_iterator begin() const noexcept

返回第一个元素的常量迭代器。

end

iterator end() noexcept

返回最后一个元素的前面一个元素的迭代器。

const_iterator end() const noexcept

返回最后一个元素的前面一个元素的常量迭代器。

rbegin

reverse_iterator rbegin() noexcept

返回最后一个元素的反向迭代器。

const_reverse_iterator rbegin() const noexcept

返回最后一个元素的常量反向迭代器。

rend

reverse_iterator rend() noexcept

返回第一个元素的前面一个元素的反向迭代器。

const_reverse_iterator rend() const noexcept

返回第一个元素的前面一个元素的常量反向迭代器。

cbegin

const_iterator cbegin() const noexcept

begin 的常量版本。

cend

const_iterator cend() const noexcept

end 的常量版本。

crbegin

const_reverse_iterator crbegin() const noexcept

rbegin 的常量版本。

crend

const_reverse_iterator crend() const noexcept

rend 的常量版本。

size

size_type size() const noexcept

返回 map 存储的元素的数量。

max_size

size_type max_size() const noexcept

返回最大能够存储的元素的数量。

empty

bool empty() const noexcept

返回 map 是否是空的。

get_allocator

allocator_type get_allocator() const noexcept

返回内存分配器。

key_comp

key_compare key_comp() const

返回键比较器。

value_comp

value_compare value_comp() const

返回值比较器。

重载运算符

operator=

map& operator=(const map& _Right)

将另一个右值内的元素拷贝到当前 map。

map& operator=(initializer_list<value_type> _Ilist)

将 map 赋值为列表。

operator[]

mapped_type& operator[](const key_type& _Keyval)

返回键对应的值的引用,如果没找到则初始化一个。

mapped_type& operator[](key_type&& _Keyval)

返回键(右值引用)对应的值的引用,如果没找到则初始化一个。

非成员函数

swap

swap(map<_Kty, _Ty, _Pr, _Alloc>& _Left, map<_Kty, _Ty, _Pr, _Alloc>& _Right) noexcept(noexcept(_Left.swap(_Right)))

交换两个 map。

非成员重载运算符

operator==

bool operator==(const map<_Kty, _Ty, _Pr, _Alloc>& _Left, const map<_Kty, _Ty, _Pr, _Alloc>& _Right)

判断两个 map 是否相等。

operator!=

bool operator!=(const map<_Kty, _Ty, _Pr, _Alloc>& _Left, const map<_Kty, _Ty, _Pr, _Alloc>& _Right)

判断两个 map 是否不相等。

bool operator<(const map<_Kty, _Ty, _Pr, _Alloc>& _Left, const map<_Kty, _Ty, _Pr, _Alloc>& _Right)

返回是否小于。

operator>

bool operator>(const map<_Kty, _Ty, _Pr, _Alloc>& _Left, const map<_Kty, _Ty, _Pr, _Alloc>& _Right)

返回是否大于。

operator

bool operator<=(const map<_Kty, _Ty, _Pr, _Alloc>& _Left, const map<_Kty, _Ty, _Pr, _Alloc>& _Right)

返回是否小于等于。

operator>=

bool operator>=(const map<_Kty, _Ty, _Pr, _Alloc>& _Left, const map<_Kty, _Ty, _Pr, _Alloc>& _Right)