C++STL关联容器
map
搜索、移除和插入操作具有对数复杂度。
通常被实现为红黑树或二叉搜索树
map
容器通常比 unordered_map
容器通过它们的键访问单个元素慢,但它们允许根据它们的顺序直接迭代子集。
map 容器中的 value_type
是 pair<const key_type,mapped_type>
的别名,mapped_type
是第二个模板参数 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());
~map
~map();
operator=
新内容分配给容器,替换其当前内容
copy (1)
map& operator= (const map& x);
move (2)
map& operator= (map&& x);
initializer list (3)
map& operator= (initializer_list<value_type> il);
Iterators
begin,rbegin,cbegin,crbegin
end,rend,cend,crend
Capacity
empty
bool empty() const noexcept;
size
size_type size() const noexcept;
max_size
size_type max_size() const noexcept;
Element access
operator[]
匹配则返回对应值引用,不匹配则插入新元素并返回对应映射值(值若无指定则是默认构造函数赋值)的引用。
mapped_type& operator[] (const key_type& k);
mapped_type& operator[] (key_type&& k);
at(C++11)
不匹配时与operator[]
有相同的行为,只不过不匹配时会抛出异常。
mapped_type& at (const key_type& k);
const mapped_type& at (const key_type& k) const;
Modifiers
insert
如果插入元素的键已存在则不插入,并返回现有元素的迭代器。
single element (1)
pair<iterator,bool> insert (const value_type& val);
template <class P> pair<iterator,bool> insert (P&& val);
with hint (2)
iterator insert (const_iterator position, const value_type& val);
template <class P> iterator insert (const_iterator position, P&& val);
range (3)
template <class InputIterator>
void insert (InputIterator first, InputIterator last);
initializer list (4)
void insert (initializer_list<value_type> il);
erase
删除单个元素或一系列元素 [first,last)
(左闭右开)
(1)
iterator erase (const_iterator position);
(2)
size_type erase (const key_type& k);
(3)
iterator erase (const_iterator first, const_iterator last);
swap
void swap (map& x);
clear
void clear() noexcept;
emplace
template <class... Args>
pair<iterator,bool> emplace (Args&&... args);
emplace_hint
template <class... Args>
iterator emplace_hint (const_iterator position, Args&&... args);
Observers
key_comp
key_compare key_comp() const;
value_comp
value_compare value_comp() const;
Operations
find
找不到返回map::end
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
count
统计键相等的元素数量,由于键唯一所以只能是1
和0
size_type count (const key_type& k) const;
lower_bound
同set
iterator lower_bound (const key_type& k);
const_iterator lower_bound (const key_type& k) const;
upper_bound
同set
iterator upper_bound (const key_type& k);
const_iterator upper_bound (const key_type& k) const;
equal_range
同set
pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
pair<iterator,iterator> equal_range (const key_type& k);
Allocator
get_allocator
allocator_type get_allocator() const noexcept;