map、hash_map、unordered_map 的思考
#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的时候可以:
- vector<string> L;
unordered_map<string,int> dictL;- for(int i = ; i< L.size(); i++)
- 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
下面是它比较的代码
- #include <iostream>
- #include <stdlib.h>
- #include <Windows.h>
- #include <map>
- #include <hash_map>
- #include <unordered_map>
- #include <algorithm>
- bool MapTest()
- {
- const unsigned int NUM_COUNT = 0xffffff; // 数组长度
- const int DEFAULT_VALUE = ; // 键值
- const unsigned int NUM_RANGE = 0xffffff; // 随机数范围的最大值
- int* szNumA = new int[NUM_COUNT];
- int* szNumB = new int[NUM_COUNT];
- srand(::GetTickCount());
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- szNumA[uiNum] = (rand() * rand()) % NUM_RANGE;
- szNumB[uiNum] = szNumA[uiNum];
- }
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- std::swap(szNumB[uiNum], szNumB[(rand() * rand()) % NUM_COUNT]);
- }
- std::map<int, int> mapNum;
- std::hash_map<int, int> hMapNum;
- std::unordered_map<int, int> unMapNum;
- DWORD dwMap, dwHMap, dwUnMap;
- // 插入测试
- dwMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- mapNum.insert(std::map<int, int>::value_type(szNumA[uiNum], ));
- }
- dwMap = ::GetTickCount() - dwMap;
- dwHMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- hMapNum.insert(std::hash_map<int, int>::value_type(szNumA[uiNum], ));
- }
- dwHMap = ::GetTickCount() - dwHMap;
- dwUnMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- unMapNum.insert(std::unordered_map<int, int>::value_type(szNumA[uiNum], ));
- }
- dwUnMap = ::GetTickCount() - dwUnMap;
- std::cout << "insert time of map is :" << dwMap << "ms" <<std::endl;
- std::cout << "insert time of hash_map is :" << dwHMap << "ms" <<std::endl;
- std::cout << "insert time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl;
- // 查找测试
- dwMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- mapNum.find(szNumB[uiNum]);
- }
- dwMap = ::GetTickCount() - dwMap;
- dwHMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- hMapNum.find(szNumB[uiNum]);
- }
- dwHMap = ::GetTickCount() - dwHMap;
- dwUnMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- unMapNum.find(szNumB[uiNum]);
- }
- dwUnMap = ::GetTickCount() - dwUnMap;
- std::cout << "search time of map is :" << dwMap << "ms" <<std::endl;
- std::cout << "search time of hash_map is :" << dwHMap << "ms" <<std::endl;
- std::cout << "search time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl;
- // 删除测试
- dwMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- mapNum.erase(szNumB[uiNum]);
- }
- dwMap = ::GetTickCount() - dwMap;
- dwHMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- hMapNum.erase(szNumB[uiNum]);
- }
- dwHMap = ::GetTickCount() - dwHMap;
- dwUnMap = ::GetTickCount();
- for (unsigned int uiNum = ; uiNum < NUM_COUNT; ++uiNum)
- {
- unMapNum.erase(szNumB[uiNum]);
- }
- dwUnMap = ::GetTickCount() - dwUnMap;
- std::cout << "delete time of map is :" << dwMap << "ms" <<std::endl;
- std::cout << "delete time of hash_map is :" << dwHMap << "ms" <<std::endl;
- std::cout << "delete time of unordered_map is :" << dwUnMap << "ms" <<std::endl << std::endl;
- delete [] szNumA;
- delete [] szNumB;
- return true;
- }
- int main(int argc, char* argv[])
- {
- MapTest();
- system("pause");
- return ;
- }
map、hash_map、unordered_map 的思考的更多相关文章
- map 与 unordered_map
两者效率对比: #include <iostream> #include <string> #include <map> #include <unordere ...
- map和unordered_map的差别和使用
map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/billcyj/article/details/7 ...
- 【转】Map 与 Unordered_map
map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...
- C++ map与unordered_map
map与unordered_map对比 map unordered_map 红黑树(非严格二叉平衡搜索树)实现 哈希表实现 有序 无序 -- 查找时间复杂度为O(1),非常快 空间消耗较大 空间消耗较 ...
- STL中的map和unordered_map
STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...
- C++中map和unordered_map的用法
1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...
- map和unordered_map使用小结
map和unordered_map unordered_map简介: #include <cstdio> #include <iostream> #include <un ...
- 原 c++中map与unordered_map的区别
c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...
- 关于c++ STL map 和 unordered_map 的效率的对比测试
本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...
- Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序
写程序时,面临用Map还是unordered_map,总是很纠结,于是写了个程序进行测试 Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序 简单数据(4 Byte) 首先 ...
随机推荐
- 用Python学分析 - 单因素方差分析
单因素方差分析(One-Way Analysis of Variance) 判断控制变量是否对观测变量产生了显著影响 分析步骤 1. 建立检验假设 - H0:不同因子水平间的均值无差异 - H1:不同 ...
- proc的妙用
今天在在公司做网络驱动开发测试时,随机包出现收包计数停止的现象,当时怀疑是DMA rx buffer不足导致,想通过对比收发包正常和收发包不正常是DMA相关寄存器的情况. 后跟踪代码,若在收发包里面增 ...
- git使用问题整理
git访问远端仓库报"fatal: Authentication failed for"错误的,可能原因是账户密码变更,git配置了使用creditial helper,所以需要取 ...
- vue-router2.0组件复用
在使用vue-router1.x时我们知道对于路由 a/b/c 和 a/b/d , 组件a和组件b将会复用 .具体可以参考:https://github.com/vuejs/vue-router ...
- Asp.net自定义控件开发任我行(1)-笑傲江湖
1.引言 参加工作5个月了,来到一家小公司,有几只老鸟带我,但不是我公司的,几个礼拜才来一次.来到公司做的第一个项目是web项目,里面有很多的重复代码,页面代码都是千篇一律,你这人也太水了吧,垃圾代码 ...
- 谋哥:转型之痒与App推广之痛
昨天<重庆今日教育>的副主编汪熙坤老师先加我微信,谋哥的微信每天有几十个不同领域的朋友加.几句客套后,他马上就直奔主题了.为什么这么着急呢?是因为危机感,是因为感受到了互联网给传统纸媒带来 ...
- IOS开发学习笔记017-第一个IOS应用
第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...
- RSA进阶之低加密指数攻击
适用场景: n很大,4000多位,e很小,e=3 一般来说,e选取65537.但是在RSA加密的时候有可能会选用e=3(不要问为什么,因为选取e =3省时省力,并且他可能觉得n在4000多位是很安全的 ...
- selenium 浏览器驱动下载地址
谷歌浏览器驱动下载http://chromedriver.storage.googleapis.com/index.html 火狐浏览器驱动下载http://ftp.mozilla.org/pub/f ...
- 项目太多工作环境互相干扰?virtualenv 一招教你轻松解决。
写在之前 在上一篇文章 安装的 Python 版本太多互相干扰?以后再也不用担心这个问题了. 中我给大家介绍了一个 Python 版本的管理工具「pyenv」,可以很容易的安装不同的 Python 版 ...