#include <map>

map<string,int> dict;

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

dict.insert(make_pair("abc",1));

dict.count("mn"); 看看dict中含有 mn的个数,因为元素是唯一的,所以这个返回值只有两种情况,0或者1. (multi_map就不是这样啦)

dict.find("mn");如果不存在就返回 dict.end(),如果存在就返回指向该元素的 iterator

dict["pq"]++ 相当于取出 value 来进行 ++ 再存回去,下标访问不存在的元素,将导致在map中添加一个新的元素,新的键就是该下标, value的值为默认值。

所以建立一个map的时候可以:

  1. vector<string> L;
    unordered_map<string,int> dictL;
  2. for(int i = ; i< L.size(); i++)
  3. dictL[L[i]] += ;

删除元素: dict.eraser("abc"); 返回值为删除元素的个数

dict.eraser(itr); 删除 itr 指向的元素,并且 itr 所指向元素必须是存在的,不能是 dict.end(). 这种形式的返回值为 void

遍历: for(map<string, int>::iterator itr = dict.begin(); itr != dict.end(); itr++)

cout<< itr->first <<"  " << itr->second <<endl;

map中的元素是按照健,有序的.

#include<unordered_map>

unordered_map是基于hash实现的。

unordered_map的插入、删除、查找 性能都优于 hash_map 优于 map,所以首选为 unordered_map.

它的缺陷是元素是无序的,当使用时候需要元素是有序的时候,不可以用它。

性能比较参考:http://keary.cn/?p=779

下面是它比较的代码

  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4. #include <map>
  5. #include <hash_map>
  6. #include <unordered_map>
  7. #include <algorithm>
  8.  
  9. bool MapTest()
  10. {
  11. const unsigned int NUM_COUNT = 0xffffff; // 数组长度
  12. const int DEFAULT_VALUE = ; // 键值
  13. const unsigned int NUM_RANGE = 0xffffff; // 随机数范围的最大值
  14.  
  15. int* szNumA = new int[NUM_COUNT];
  16. int* szNumB = new int[NUM_COUNT];
  17.  
  18. srand(::GetTickCount());
  19.  
  20. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  21. {
  22. szNumA[uiNum] = (rand() * rand()) % NUM_RANGE;
  23. szNumB[uiNum] = szNumA[uiNum];
  24. }
  25. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  26. {
  27. std::swap(szNumB[uiNum], szNumB[(rand() * rand()) % NUM_COUNT]);
  28. }
  29.  
  30. std::map<int, int> mapNum;
  31. std::hash_map<int, int> hMapNum;
  32. std::unordered_map<int, int> unMapNum;
  33.  
  34. DWORD dwMap, dwHMap, dwUnMap;
  35.  
  36. // 插入测试
  37. dwMap = ::GetTickCount();
  38. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  39. {
  40. mapNum.insert(std::map<int, int>::value_type(szNumA[uiNum], ));
  41. }
  42. dwMap = ::GetTickCount() - dwMap;
  43.  
  44. dwHMap = ::GetTickCount();
  45. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  46. {
  47. hMapNum.insert(std::hash_map<int, int>::value_type(szNumA[uiNum], ));
  48. }
  49. dwHMap = ::GetTickCount() - dwHMap;
  50.  
  51. dwUnMap = ::GetTickCount();
  52. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  53. {
  54. unMapNum.insert(std::unordered_map<int, int>::value_type(szNumA[uiNum], ));
  55. }
  56. dwUnMap = ::GetTickCount() - dwUnMap;
  57.  
  58. std::cout << "insert time of map is :" << dwMap << "ms" <<std::endl;
  59. std::cout << "insert time of hash_map is :" << dwHMap << "ms" <<std::endl;
  60. std::cout << "insert time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl;
  61.  
  62. // 查找测试
  63. dwMap = ::GetTickCount();
  64. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  65. {
  66. mapNum.find(szNumB[uiNum]);
  67. }
  68. dwMap = ::GetTickCount() - dwMap;
  69.  
  70. dwHMap = ::GetTickCount();
  71. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  72. {
  73. hMapNum.find(szNumB[uiNum]);
  74. }
  75. dwHMap = ::GetTickCount() - dwHMap;
  76.  
  77. dwUnMap = ::GetTickCount();
  78. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  79. {
  80. unMapNum.find(szNumB[uiNum]);
  81. }
  82. dwUnMap = ::GetTickCount() - dwUnMap;
  83.  
  84. std::cout << "search time of map is :" << dwMap << "ms" <<std::endl;
  85. std::cout << "search time of hash_map is :" << dwHMap << "ms" <<std::endl;
  86. std::cout << "search time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl;
  87.  
  88. // 删除测试
  89. dwMap = ::GetTickCount();
  90. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  91. {
  92. mapNum.erase(szNumB[uiNum]);
  93. }
  94. dwMap = ::GetTickCount() - dwMap;
  95.  
  96. dwHMap = ::GetTickCount();
  97. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  98. {
  99. hMapNum.erase(szNumB[uiNum]);
  100. }
  101. dwHMap = ::GetTickCount() - dwHMap;
  102.  
  103. dwUnMap = ::GetTickCount();
  104. for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
  105. {
  106. unMapNum.erase(szNumB[uiNum]);
  107. }
  108. dwUnMap = ::GetTickCount() - dwUnMap;
  109.  
  110. std::cout << "delete time of map is :" << dwMap << "ms" <<std::endl;
  111. std::cout << "delete time of hash_map is :" << dwHMap << "ms" <<std::endl;
  112. std::cout << "delete time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl;
  113.  
  114. delete [] szNumA;
  115. delete [] szNumB;
  116.  
  117. return true;
  118. }
  119.  
  120. int main(int argc, char* argv[])
  121. {
  122. MapTest();
  123.  
  124. system("pause");
  125. return ;
  126. }

map、hash_map、unordered_map 的思考的更多相关文章

  1. map 与 unordered_map

    两者效率对比: #include <iostream> #include <string> #include <map> #include <unordere ...

  2. map和unordered_map的差别和使用

    map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/billcyj/article/details/7 ...

  3. 【转】Map 与 Unordered_map

    map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...

  4. C++ map与unordered_map

    map与unordered_map对比 map unordered_map 红黑树(非严格二叉平衡搜索树)实现 哈希表实现 有序 无序 -- 查找时间复杂度为O(1),非常快 空间消耗较大 空间消耗较 ...

  5. STL中的map和unordered_map

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

  6. C++中map和unordered_map的用法

    1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...

  7. map和unordered_map使用小结

    map和unordered_map unordered_map简介: #include <cstdio> #include <iostream> #include <un ...

  8. 原 c++中map与unordered_map的区别

    c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...

  9. 关于c++ STL map 和 unordered_map 的效率的对比测试

    本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...

  10. Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序

    写程序时,面临用Map还是unordered_map,总是很纠结,于是写了个程序进行测试 Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序 简单数据(4 Byte) 首先 ...

随机推荐

  1. 用Python学分析 - 单因素方差分析

    单因素方差分析(One-Way Analysis of Variance) 判断控制变量是否对观测变量产生了显著影响 分析步骤 1. 建立检验假设 - H0:不同因子水平间的均值无差异 - H1:不同 ...

  2. proc的妙用

    今天在在公司做网络驱动开发测试时,随机包出现收包计数停止的现象,当时怀疑是DMA rx buffer不足导致,想通过对比收发包正常和收发包不正常是DMA相关寄存器的情况. 后跟踪代码,若在收发包里面增 ...

  3. git使用问题整理

    git访问远端仓库报"fatal: Authentication failed for"错误的,可能原因是账户密码变更,git配置了使用creditial helper,所以需要取 ...

  4. vue-router2.0组件复用

    在使用vue-router1.x时我们知道对于路由 a/b/c  和  a/b/d ,  组件a和组件b将会复用 .具体可以参考:https://github.com/vuejs/vue-router ...

  5. Asp.net自定义控件开发任我行(1)-笑傲江湖

    1.引言 参加工作5个月了,来到一家小公司,有几只老鸟带我,但不是我公司的,几个礼拜才来一次.来到公司做的第一个项目是web项目,里面有很多的重复代码,页面代码都是千篇一律,你这人也太水了吧,垃圾代码 ...

  6. 谋哥:转型之痒与App推广之痛

    昨天<重庆今日教育>的副主编汪熙坤老师先加我微信,谋哥的微信每天有几十个不同领域的朋友加.几句客套后,他马上就直奔主题了.为什么这么着急呢?是因为危机感,是因为感受到了互联网给传统纸媒带来 ...

  7. IOS开发学习笔记017-第一个IOS应用

    第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...

  8. RSA进阶之低加密指数攻击

    适用场景: n很大,4000多位,e很小,e=3 一般来说,e选取65537.但是在RSA加密的时候有可能会选用e=3(不要问为什么,因为选取e =3省时省力,并且他可能觉得n在4000多位是很安全的 ...

  9. selenium 浏览器驱动下载地址

    谷歌浏览器驱动下载http://chromedriver.storage.googleapis.com/index.html 火狐浏览器驱动下载http://ftp.mozilla.org/pub/f ...

  10. 项目太多工作环境互相干扰?virtualenv 一招教你轻松解决。

    写在之前 在上一篇文章 安装的 Python 版本太多互相干扰?以后再也不用担心这个问题了. 中我给大家介绍了一个 Python 版本的管理工具「pyenv」,可以很容易的安装不同的 Python 版 ...