C++11中新特性之:unordered_map
unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value。
不同的是unordered_map不会根据key的大小进行排序,存储时是根据key的hash值判断元素是否相同,即unordered_map内部元素是无序的,而map中的元素是按照二叉搜索树存储,进行中序遍历会得到有序遍历。
所 以使用时map的key需要定义operator<。而unordered_map需要定义hash_value函数并且重载 operator==。但是很多系统内置的数据类型都自带这些,那么如果是自定义类型,那么就需要自己重载operator<或者 hash_value()了。
结论:如果需要内部元素自动排序,使用map,不需要排序使用unordered_map
C++0X为什么不把unordered_map定义为hash_map呢?那是因为在新标准出现之前很多库厂商已经暂用了hash_map这个名词。因此为了向前兼容不得不定义新的unordered_map。
函数原型
template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash<Key>, // unordered_map::hasher
class Pred = equal_to<Key>, // unordered_map::key_equal
class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type
>
class unordered_map; Key表示建的类型
T表示键映射到的hash值的类型
Hash是一个接受一个参数且类型要与Key兼容的函数对象。返回值为T型。注意,参数是const引用, 函数是const
Pred是一个接受两个参数且其类型与Key兼容的函数对象。返回值为BOOL型。注意,参数是const引用, 函数是const 成员函数:
Member functions
- (constructor)
- Construct unordered_map (public member function )
- (destructor)
- Destroy unordered map (public member function)
- operator=
- Assign content (public member function )
Capacity
- empty
- Test whether container is empty (public member function)
- size
- Return container size (public member function)
- max_size
- Return maximum size (public member function)
Iterators
- begin
- Return iterator to beginning (public member function)
- end
- Return iterator to end (public member function)
- cbegin
- Return const_iterator to beginning (public member function)
- cend
- Return const_iterator to end (public member function)
Element access
- operator[]
- Access element (public member function )
- at
- Access element (public member function)
Element lookup
- find
- Get iterator to element (public member function)
- count
- Count elements with a specific key (public member function )
- equal_range
- Get range of elements with specific key (public member function)
Modifiers
- emplace
- Construct and insert element (public member function )
- emplace_hint
- Construct and insert element with hint (public member function )
- insert
- Insert elements (public member function )
- erase
- Erase elements (public member function )
- clear
- Clear content (public member function )
- swap
- Swap content (public member function)
Buckets
- bucket_count
- Return number of buckets (public member function)
- max_bucket_count
- Return maximum number of buckets (public member function)
- bucket_size
- Return bucket size (public member type)
- bucket
- Locate element's bucket (public member function)
Hash policy
- load_factor
- Return load factor (public member function)
- max_load_factor
- Get or set maximum load factor (public member function )
- rehash
- Set number of buckets (public member function )
- reserve
- Request a capacity change (public member function)
Observers
- hash_function
- Get hash function (public member type)
- key_eq
- Get key equivalence predicate (public member type)
- get_allocator
- Get allocator (public member function)
C++11中新特性之:unordered_map的更多相关文章
- C++11中新特性之:initializer_list详解
C++11提供的新类型,定义在<initializer_list>头文件中. template< class T > class initializer_list; 先说它的用 ...
- C++11中新特性之:lambda 表达式
首先摆出Lambda表达式语法 lambda-expression: lambda-introducer lambda-declaratoropt compound-statementlambda-i ...
- C++反射机制:可变参数模板实现C++反射(使用C++11的新特性--可变模版参数,只根据类的名字(字符串)创建类的实例。在Nebula高性能网络框架中大量应用)
1. 概要 本文描述一个通过C++可变参数模板实现C++反射机制的方法.该方法非常实用,在Nebula高性能网络框架中大量应用,实现了非常强大的动态加载动态创建功能.Nebula框架在码云的仓库地 ...
- Qt5 中对 C++11 一些新特性的封装
在 Qt5 中,提供更多 C++11 的特性支持,接下来我们将进行详细的说明. slots (槽) 的 Lambda 表达式 Lambda表达式 是 C++11 中的一个新语法,允许定义匿名函数.匿名 ...
- 【Qt开发】Qt5 中对 C++11 一些新特性的封装
C++11 是现在的 C++ 标准的名称,C++11 为 C++ 语言带来很多新特性. 而 Qt 4.8 是 Qt 首个在其 API 中开始使用一些新的 C++11 特性的版本,我之前写过一篇博文:C ...
- 【C++11】新特性——auto的使用
[C++11]新特性——auto的使用 C++11中引入的auto主要有两种用途:自动类型推断和返回值占位.auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除.前后 ...
- 【C++11】新特性——Lambda函数
本篇文章由:http://www.sollyu.com/c11-new-lambda-function/ 文章列表 本文章为系列文章 [C++11]新特性--auto的使用 http://www.so ...
- atitit.Oracle 9 10 11 12新特性attilax总结
atitit.Oracle 9 10 11 12新特性 1. ORACLE 11G新特性 1 1.1. oracle11G新特性 1 1.2. 审计 1 1.3. 1. 审计简介 1 1.4. ...
- c++11的新特性
好奇心来源于下面的一段代码, 一个是unordered_map, 这是c++11新加的container. 另外还有unordered_set, unordered_multimap, unorder ...
随机推荐
- hdoj 2802 F(N)【递推 规律】
F(N) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Android:从程序员到架构师之路Ⅰ
一般而言,人们大多先学开发(代码)的技术,随后才学(架构)设计的方法.然而,在实际做事时,却是先设计,随后才写出代码来.敏捷过程则让设计与写码迭代循环下去,一直到完成为止.在本课程里,就遵循敏捷的迭代 ...
- Android Fragment之间传值
首先来介绍的是我们的默认的布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- Count属性(行数 @)
在PS2.0中,如果返回值为空,则count也会返回空(vm.txt内容为空),如下: (gc d:\vm.txt).count 如果加上@的话,会返回0 @(gc d:\vm.txt).count ...
- 收集磁盘分区信息(总量、可用、已用、百分比)导出到csv
#############################脚本功能及说明##################################################该脚本用来收集磁盘分区总大小 ...
- [转]非常好的vsftpd安装于配置
环境:CentOS 5.0 操作系统一.安装:1.安装Vsftpd服务相关部件:[root@KcentOS5 ~]# yum install vsftpd*Dependencies Resolved= ...
- Apache Mina 2.x 框架+源码分析
源码下载 http://www.apache.org/dyn/closer.cgi/mina/mina/2.0.9/apache-mina-2.0.9-src.tar.gz 整体架构 核心过程(IoA ...
- Java开源内容管理CMS系统J4CMS支持静态化直接ftp上传到你的空间了
页面可直接编辑的cms系统j4cms能够直接支持使用ftp 输出 html到你的空间了 真正的静态化 这是静态化后的站点 http://www.j4cms.com/ 源代码上传至csdn了,能够下载源 ...
- oracle db mos文章 翻译系列
http://blog.csdn.net/msdnchina/article/details/38377125
- How to create a PPPoE Server on Ubuntu? (Untested)
How to create a PPPoE Server on Ubuntu? March 30, 2011 coder_commenter Leave a comment Go to comment ...