栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++STL关联容器02-map

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++STL关联容器02-map

C++STL关联容器
  • map
    • (constructor)
    • ~map
    • operator=
  • Iterators
    • begin,rbegin,cbegin,crbegin
    • end,rend,cend,crend
  • Capacity
    • empty
    • size
    • max_size
  • Element access
    • operator[]
    • at(C++11)
  • Modifiers
    • insert
    • erase
    • swap
    • clear
    • emplace
    • emplace_hint
  • Observers
    • key_comp
    • value_comp
  • Operations
    • find
    • count
    • lower_bound
    • upper_bound
    • equal_range
  • Allocator
    • get_allocator

map

搜索、移除和插入操作具有对数复杂度。
通常被实现为红黑树或二叉搜索树

map 容器通常比 unordered_map 容器通过它们的键访问单个元素慢,但它们允许根据它们的顺序直接迭代子集。

map 容器中的 value_type 是 pair 的别名,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 
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 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 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 insert (const value_type& val);
template  pair insert (P&& val);
with hint (2)	
iterator insert (const_iterator position, const value_type& val);
template  iterator insert (const_iterator position, P&& val);
range (3)	
template 
  void insert (InputIterator first, InputIterator last);
initializer list (4)	
void insert (initializer_list 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 
pair emplace (Args&&... args);
emplace_hint
template 
  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 equal_range (const key_type& k) const;
pair             equal_range (const key_type& k);
Allocator get_allocator
allocator_type get_allocator() const noexcept;
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/352761.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号