zoukankan      html  css  js  c++  java
  • 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

    
    

    Capacity

    
    

    Iterators

    
    

    Element access

    
    

    Element lookup

    
    

    Modifiers

    
    

    Buckets

    
    

    Hash policy

    
    

    Observers















  • 相关阅读:
    python 找到列表中满足条件的元素
    android activity动画anim
    Maven实战(Maven+Nexus建立私服【Linux系统】)
    linux命令
    服务端工程师入门与进阶 Java 版
    jvm字节占用空间分析
    Spark Streaming容错的改进和零数据丢失
    Spark分布式计算和RDD模型研究
    Apache Curator入门实战
    spark简单总结—短小精悍
  • 原文地址:https://www.cnblogs.com/lysuns/p/4324535.html
Copyright © 2011-2022 走看看