map介绍

Map是STL[1]的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

hash_map介绍

hash_map基于hash table(哈希表)。 哈希表最大的优点,就是把数据的存储和查找消耗的时间大大降低,几乎可以看成是常数时间;而代价仅仅是消耗比较多的内存。然而在当前可利用内存越来越多的情况下,用空间换时间的做法是值得的。另外,编码比较容易也是它的特点之一。

其基本原理是:使用一个下标范围比较大的数组来存储元素。可以设计一个函数(哈希函数,也叫做散列函数),使得每个元素的关键字都与一个函数值(即数组下标,hash值)相对应,于是用这个数组单元来存储这个元素;也可以简单的理解为,按照关键字为每一个元素“分类”,然后将这个元素存储在相应“类”所对应的地方,称为桶。

但是,不能够保证每个元素的关键字与函数值是一一对应的,因此极有可能出现对于不同的元素,却计算出了相同的函数值,这样就产生了“冲突”,换句话说,就是把不同的元素分在了相同的“类”之中。 总的来说,“直接定址”与“解决冲突”是哈希表的两大特点。

hash_map,首先分配一大片内存,形成许多桶。是利用hash函数,对key进行映射到不同区域(桶)进行保存。其插入过程是:

1.得到key 
2.通过hash函数得到hash值 
3.得到桶号(一般都为hash值对桶数求模) 
4.存放key和value在桶内。 
其取值过程是: 
1.得到key 
2.通过hash函数得到hash值 
3.得到桶号(一般都为hash值对桶数求模) 
4.比较桶的内部元素是否与key相等,若都不相等,则没有找到。 
5.取出相等的记录的value。 
hash_map中直接地址用hash函数生成,解决冲突,用比较函数解决。这里可以看出,如果每个桶内部只有一个元素,那么查找的时候只有一次比较。当许多桶内没有值时,许多查询就会更快了(指查不到的时候).

由此可见,要实现哈希表, 和用户相关的是:hash函数和比较函数。这两个参数刚好是我们在使用hash_map时需要指定的参数。

unordered_map介绍

Unordered maps are associative containers that store elements formed by the combination of a key value and amapped value, and which allows for fast retrieval of individual elements based on their keys.

In an unordered_map, the key value is generally used to uniquely identify the element, while the mapped value is an object with the content associated to this key. Types of key and mapped value may differ.

Internally, the elements in the unordered_map are not sorted in any particular order with respect to either theirkey or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).

unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

Unordered maps implement the direct access operator (operator[]) which allows for direct access of themapped value using its key value as argument.

unordered_map与map的区别

boost::unordered_map, 它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合适的位置插入到树中。所以,如果对map进行遍历(中序遍历)的话,输出的结果是有序的。顺序就是按照operator< 定义的大小排序。
而boost::unordered_map是计算元素的Hash值,根据Hash值判断元素是否相同。所以,对unordered_map进行遍历,结果是无序的。
用法的区别就是,stl::map 的key需要定义operator< 。 而boost::unordered_map需要定义hash_value函数并且重载operator==。对于内置类型,如string,这些都不用操心。对于自定义的类型做key,就需要自己重载operator< 或者hash_value()了。 
最后,说,当不需要结果排好序时,最好用unordered_map。
其实,stl::map对于与java中的TreeMap,而boost::unordered_map对应于java中的HashMap。

运行结果

记录数N=100000时,结果如下:

Map类型

插入耗时,单位秒

插入加遍历耗时,单位秒

内存占用情况

map

0.110705

0.171859

5,780kB

hash_map

0.079074

0.091751

5,760kB

unordered_map

0.041311

0.050298

5,216kB

记录数N=1000000时,结果如下:

Map类型

插入耗时,单位秒

插入加遍历耗时,单位秒

内存占用情况

map

1.22678

1.95435

47,960kB

hash_map

0.684772

0.814646

44,632kB

unordered_map

0.311155

0.386898

40,604kB

记录数N=10000000时,结果如下:

Map类型

插入耗时,单位秒

插入加遍历耗时,单位秒

内存占用情况

map

14.9517

23.9928

469,844kB

hash_map

5.93318

7.18117

411,904kB

unordered_map

3.64201

4.43355

453,920kB

记录数N=100000000时,结果如下:

Map类型

插入耗时,单位秒

插入加遍历耗时,单位秒

内存占用情况

map

167.941

251.591

4,688,692kB

hash_map

46.3518

57.6972

3,912,632kB

unordered_map

28.359

35.122

4,3012,56kB

结果分析

运行效率方面:unordered_map最高,hash_map其次,而map效率最低

占用内存方面:hash_map内存占用最低,unordered_map其次,而map占用最高

stl::map

  1. #include<string>
  2. #include<iostream>
  3. #include<map>
  4. using namespace std;
  5. struct person
  6. {
  7. string name;
  8. int age;
  9. person(string name, int age)
  10. {
  11. this->name =  name;
  12. this->age = age;
  13. }
  14. bool operator < (const person& p) const
  15. {
  16. return this->age < p.age;
  17. }
  18. };
  19. map<person,int> m;
  20. int main()
  21. {
  22. person p1("Tom1",20);
  23. person p2("Tom2",22);
  24. person p3("Tom3",22);
  25. person p4("Tom4",23);
  26. person p5("Tom5",24);
  27. m.insert(make_pair(p3, 100));
  28. m.insert(make_pair(p4, 100));
  29. m.insert(make_pair(p5, 100));
  30. m.insert(make_pair(p1, 100));
  31. m.insert(make_pair(p2, 100));
  32. for(map<person, int>::iterator iter = m.begin(); iter != m.end(); iter++)
  33. {
  34. cout<<iter->first.name<<"\t"<<iter->first.age<<endl;
  35. }
  36. return 0;
  37. }

output:

Tom1    20
Tom3    22
Tom4    23
Tom5    24

operator<的重载一定要定义成const。因为map内部实现时调用operator<的函数好像是const。

由于operator<比较的只是age,所以因为Tom2和Tom3的age相同,所以最终结果里面只有Tom3,没有Tom2

boost::unordered_map

  1. #include<string>
  2. #include<iostream>
  3. #include<boost/unordered_map.hpp>
  4. using namespace std;
  5. struct person
  6. {
  7. string name;
  8. int age;
  9. person(string name, int age)
  10. {
  11. this->name =  name;
  12. this->age = age;
  13. }
  14. bool operator== (const person& p) const
  15. {
  16. return name==p.name && age==p.age;
  17. }
  18. };
  19. size_t hash_value(const person& p)
  20. {
  21. size_t seed = 0;
  22. boost::hash_combine(seed, boost::hash_value(p.name));
  23. boost::hash_combine(seed, boost::hash_value(p.age));
  24. return seed;
  25. }
  26. int main()
  27. {
  28. typedef boost::unordered_map<person,int> umap;
  29. umap m;
  30. person p1("Tom1",20);
  31. person p2("Tom2",22);
  32. person p3("Tom3",22);
  33. person p4("Tom4",23);
  34. person p5("Tom5",24);
  35. m.insert(umap::value_type(p3, 100));
  36. m.insert(umap::value_type(p4, 100));
  37. m.insert(umap::value_type(p5, 100));
  38. m.insert(umap::value_type(p1, 100));
  39. m.insert(umap::value_type(p2, 100));
  40. for(umap::iterator iter = m.begin(); iter != m.end(); iter++)
  41. {
  42. cout<<iter->first.name<<"\t"<<iter->first.age<<endl;
  43. }
  44. return 0;
  45. }

输出

Tom1    20
Tom5    24
Tom4    23
Tom2    22
Tom3    22

必须要自定义operator==和hash_value。 重载operator==是因为,如果两个元素的hash_value的值相同,并不能断定这两个元素就相同,必须再调用operator==。 当然,如果hash_value的值不同,就不需要调用operator==了。

map,hash_map和unordered_map 实现比较的更多相关文章

  1. hash_map vs unordered_map vs map vs unordered_set

    hash_map vs unordered_map 这两个的内部结构都是采用哈希表来实现.unordered_map在C++11的时候被引入标准库了,而hash_map没有,所以建议还是使用unord ...

  2. map、hash_map、unordered_map 的思考

    #include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...

  3. STL set multiset map multimap unordered_set unordered_map example

    I decide to write to my blogs in English. When I meet something hard to depict, I'll add some Chines ...

  4. STL中stack/queue/map以及Boost unordered_map 的使用方法

    一.stackstack 模板类的定义在<stack>头文件中.stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型 ...

  5. (转载)STL map与Boost unordered_map的比较

    原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...

  6. hash_map与unordered_map区别

    http://blog.csdn.net/blues1021/article/details/45054159

  7. c++ hash_map/unordered_map 使用

    C++中有很多中key-value形式的容器,map/hash_map/unordered_map/vector_map.下面讲述各个map的使用及其区别. map: #include <ios ...

  8. unordered_map 与 map 的对比(转)

    unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value.不同的是unordered_map不会根据key的大小进行排序, 存储时是根据key的ha ...

  9. STL中的map和unordered_map

    STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...

随机推荐

  1. [转]c/c++输入函数

    最全输入函数 c/c++ 一: c=getchar(); 功能:读入一个字符 说明:调用此函数时要求在程序的第一行有预编译命令:#include<stdio>,不过在做c++时 有#inc ...

  2. UVa 10154 - Weights and Measures

    UVa 10154 - Weights and Measures I know, up on top you are seeing great sights,  But down at the bot ...

  3. OSX 升级 vim

    善用 Homebrew 神器啊,少年!Homebrew - The missing package manager for OS X安装完成后打开终端输入: brew install vim --wi ...

  4. python下载图片

    import re import  urllib.request   def getHtml(url): page = urllib.request.urlopen(url) html = page. ...

  5. mongodb添加用户和认证

    Mongodb默认启动是不带认证,也没有账号,只要能连接上服务就可以对数据库进行各种操作,这样可不行.现在,我们得一步步开启使用用户和认证. 第一步,我们得定位到mongodb的安装目录.我本机的是C ...

  6. c#换ip代理源码

    很多朋友都想如何提高自己的网站流量,可是都没有什么好的办法 经过很长时间的研究,在C#中实现了,当然了,这部分代码其中一部分是网上的,不是原创. using System; using System. ...

  7. JS通用方法总结(一)

    /** * id数组转换为json字符串 */ function arrayTojson(arr) { var jsonIds = "["; for ( var i = 0; i ...

  8. 如何直接在github网站上更新你fork的repo?

    玩过github的人一定会在你自己的账号上fork了一些github开源项目.这些开源项目往往更新比较活跃,你今天fork用到你自己的项目中去了,过几个星期这个fork的origin可能有一些bugf ...

  9. HTML5学习总结——HTML5新增属性与表单元素

    一HTML5新增属性 1.1contcxtmcnu contextmenu的作用是指定右键菜单. <!DOCTYPE html> <html> <head> < ...

  10. Qt之模式、非模式、半模式对话框

    简述 关于"模式"和"非模式"对话框,相信大家都比较熟悉,但其中有一个可能很多人都比较陌生,介于两者之间的状态,我们称之为"半模式". 简述 ...