容器unordered_map方法总结——中文博客最清晰完整的整理

unordered_map

注:

  • *标记的为c++11新特性,构造函数默认为c++11
  • 函数原型省略了一些细节,只展示实际需要填写的情况(分配器已经隐藏了,需要可以去c++reference查看)
  • 参数栏表示传入的两个参数的意义而不是类型
  • 返回值栏表示类型而不是意义
  • 粉红色表示为c++11新特性
  • 参数若标黄,则可以不填
  • unordered_map 以下简称um

构造方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
构造函数explicit unordered_map ( size_type n = /* see below */,const hasher& hf = hasher(),const key_equal& eql = key_equal(),const allocator_type& alloc = allocator_type() );空间数量n哈希函数对象hf比较函数对象eql分配器allocdefault构建一个空间大小为n,哈希方法为hf,比较方法为eql的unordered_map
构造函数explicit unordered_map ( const allocator_type& alloc );分配器allocdefault构建一个unordered_map
构造函数unordered_map ( InputIterator first, InputIterator last,size_type n = /* see below */,const hasher& hf = hasher(),const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() );指向开始的迭代器first,指向结束的迭代器last,空间大小n哈希函数对象hf比较器函数对象eql分配器allocdefault构建一个空间大小为n,哈希方法为hf,比较方法为eql,内容为从first开始到last结束的元素的拷贝的unordered_map
构造函数unordered_map ( const unordered_map& ump );需要拷贝的um的引用umpdefault构建一个unordered_map,内容与ump相同
构造函数unordered_map ( const unordered_map& ump, const allocator_type& alloc );需要拷贝的um的引用ump,分配器allocdefault构建一个unordered_map,内容与ump相同
构造函数unordered_map ( unordered_map&& ump );需要拷贝的um的引用的引用umpdefault构建一个unordered_map,内容与ump相同
构造函数unordered_map ( unordered_map&& ump, const allocator_type& alloc );需要拷贝的um的引用的引用ump,分配器allocdefault构建一个unordered_map,内容与ump相同
构造函数unordered_map ( initializer_list<value_type> il,size_type n = /* see below */,const hasher& hf = hasher(),const key_equal& eql = key_equal(), const allocator_type& alloc = allocator_type() );已经初始化的列表il,空间数量n哈希函数对象hf比较函数对象eql分配器allocdefault构建一个空间大小为n,哈希方法为hf,比较方法为eql,内容为与il内容相同的unordered_map
析构函数~unordered_map();N/Adefault销毁unordered_map
重载=unordered_map& operator= ( const unordered_map& ump );需要赋值的um的引用umpum的引用执行复制赋值,将ump的所有元素复制到容器对象中
重载=unordered_map& operator= ( unordered_map&& ump );需要赋值的um的引用的引用umpum的引用执行移动分配,将ump内容的所有权转移给对象。
重载=unordered_map& operator= ( intitializer_list<value_type> il );需要赋值的已经初始化的列表um的引用将初始值设定项列表il的内容指定为容器对象的元素。

容量方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
emptybool empty() const noexcept;N/A布尔值返回是否为空
sizesize_type size() const noexcept;N/A非负整数返回元素的数量
max_sizesize_type max_size() const noexcept;N/A非负整数返回能容纳的最大元素数量

迭代器方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
beginiterator begin() noexcept;N/A迭代器返回指向第一个元素的迭代器
beginconst_iterator begin() const noexcept;N/A迭代器返回指向第一个元素的迭代器
beginlocal_iterator begin ( size_type n );位置n迭代器返回指向第一个元素的桶迭代器
beginconst_local_iterator begin ( size_type n ) const;位置n迭代器返回指向第一个元素的桶迭代器
enditerator end() noexcept;N/A迭代器返回指向最后一个元素的下一个位置的迭代器
endconst_iterator end() const noexcept;N/A迭代器返回指向最后一个元素的下一个位置的迭代器
endlocal_iterator end (size_type n);位置n迭代器返回指向最后一个元素的下一个位置的桶迭代器
endconst_local_iterator end (size_type n) const;位置n迭代器返回指向最后一个元素的下一个位置的桶迭代器
cbeginconst_iterator cbegin() const noexcept;N/A迭代器返回指向最后一个元素的的迭代器
cbeginconst_local_iterator cbegin ( size_type n ) const;位置n迭代器返回指向最后一个元素的的桶迭代器
cendconst_iterator cend() const noexcept;N/A迭代器返回指向第一个元素的前一个的迭代器
cendconst_local_iterator cend ( size_type n ) const;位置n迭代器返回指向第一个元素的前一个的桶迭代器

元素访问方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
重载[]mapped_type& operator[] ( const key_type& k );key索引的引用k映射元素类型返回key对应的值
重载[]mapped_type& operator[] ( key_type&& k );key索引的引用的引用k映射元素类型返回key对应的值
atmapped_type& at ( const key_type& k );key索引的引用k映射元素类型返回key对应的值
atconst mapped_type& at ( const key_type& k ) const;key索引的引用的引用k映射元素类型返回key对应的值

元素查找方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
finditerator find ( const key_type& k );索引的引用k迭代器返回k对应的位置的迭代器
findconst_iterator find ( const key_type& k ) const;索引的引用k迭代器返回k对应的位置的迭代器
countsize_type count ( const key_type& k ) const;索引的引用k非负整数返回索引为k的值数量
equal_rangepair<iterator,iterator> equal_range ( const key_type& k );索引的引用k一个包含两个迭代器pair返回包含索引为key的所有值的迭代器对
equal_rangepair<const_iterator,const_iterator> equal_range ( const key_type& k ) const;索引的引用k一个包含两个迭代器pair返回包含索引为key的所有值的迭代器对

编辑方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
emplacepair<iterator, bool> emplace ( Args&&... args );参数args……一个包含iterator与bool的pair(构造性的)插入(n个)参数
emplace_hintiterator emplace_hint ( const_iterator position, Args&&... args );表示位置的迭代器position,参数args……迭代器(构造性的)在position位置插入(n个)参数
insertpair<iterator,bool> insert ( const value_type& val );插入值val一个包含iterator与bool的pair插入一个值val
insertpair<iterator,bool> insert ( P&& val );插入值val一个包含iterator与bool的pair插入一个值val
insertiterator insert ( const_iterator hint, P&& val );插入位置hint,值val迭代器插入一个值val
insertvoid insert ( InputIterator first, InputIterator last );指向开头的迭代器first,指向结尾的迭代器last将first到last中间的元素插入um容器
insertvoid insert ( initializer_list<value_type> il );已经初始化的列表il将一个已经初始化的列表的元素插入um
eraseiterator erase ( const_iterator position );表示擦除位置的迭代器position迭代器将position位置的元素擦除
erasesize_type erase ( const key_type& k );值val非负整数将键为k的位置擦除
eraseiterator erase ( const_iterator first, const_iterator last );指向开头的迭代器first,指向结尾的迭代器last迭代器将first与last之间的内容擦除
clearvoid clear() noexcept;N/A清空容器
swapvoid swap ( unordered_map& ump );交换的um的引用 ump将容器的内容与ump中的内容交换

桶方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
bucket_countsize_type bucket_count() const noexcept;N/A非负整数返回um中的桶数
max_bucket_countsize_type max_bucket_count() const noexcept;N/A非负整数返回容器中可以保存的最大桶数
bucket_sizesize_type bucket_size ( size_type n ) const;值的类型n非负整数在桶n中的数量
bucketsize_type bucket ( const key_type& k ) const;键的类型k非负整数返回k所在的桶的编号

哈希策略方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
load_factorfloat load_factor() const noexcept;N/A浮点数返回容器的负载系数(负载系数 = 大小/ 桶数)
max_load_factorfloat max_load_factor() const noexcept;N/A浮点数返回容器的当前最大负载系数
max_load_factorvoid max_load_factor ( float z );新的最大负载系数z将z设置为无序_映射容器的新最大负载因子
rehashvoid rehash( size_type n );哈希表的最小存储桶数n将容器中的桶数设置为n或更多
reservevoid reserve ( size_type n );容器请求的最小数量将容器中的存储桶数设置为最适合包含至少n个元素的存储桶数

观测方法

方法(method)原型(prototype)参数(param)返回值(return)功能(function)
hash_functionhasher hash_function() const;N/A哈希函数对象返回容器使用的哈希函数对象
key_eqkey_equal key_eq() const;N/A比较函数对象返回容器使用的键比较函数对象
get_allocatorallocator_type get_allocator() const noexcept;N/A构造器返回分配器
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值