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#中某一个结构很复杂的类的对象存储起来,或者通过网路传输到远程的客户端程序中去, ...

  2. C# 文本文件打印类库(C#)

    我写了一个打印文本文件的类库,功能包括:打印预览.打印.打印时可以选择打印机,可以指定页码范围.调用方法非常简单:TextFilePrinter p = new TextFilePrinter(tbx ...

  3. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  4. 通过两个GPS计算两个GPS点的距离

    public static double GetDistance(double lat1, double lng1, double lat2, double lng2) { double radLat ...

  5. Python风格规范

    Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  6. 不同浏览器JS获取浏览器高度和宽度

    摘自:http://blog.csdn.net/lai_gb/archive/2009/07/04/4320956.aspx IE中: document.body.clientWidth ==> ...

  7. codeforces 430 A Points and Segments (easy)

    题意:给出n个点,m个区间,需要给这些点涂上蓝色或者红色,使得每个区间里面的点的红色的点的个数与蓝色的点的个数的差值小于1 唉,题目的标题就标注了一个easy= = 最开始做的时候对点还有区间都排序了 ...

  8. UVa 1225 Digit Counting

    题意:给出n,将前n个整数顺次写在一起,统计各个数字出现的次数. 用的最笨的办法--直接统计-- 后来发现网上的题解有先打表来做的 #include<iostream> #include& ...

  9. const,readonly 常量与只读

    Const是常量 Const在编译时会被编译为静态成员,它确定于编译时期,属类型级,通过类型来访问. 现在通过以下几种情况来说明const常量: (1)初始化 public const string  ...

  10. 字符集设置为UTF-8

    vi /etc/my.cnf [mysqld] character-set-server=utf8 [mysql] default-character-set=utf8 来自为知笔记(Wiz)