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) 首先 ...
随机推荐
- Java Web 基础(一) 基于TCP的Socket网络编程
一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...
- 静态属性property的本质和应用
一.本质 静态属性property本质就是实现了get,set,delete三种方法 class Foo: @property def AAA(self): print('get的时候运行我啊') @ ...
- UVA - 753 A Plug for UNIX(网络流)
题意 给定一些插头设备和插座,有一些方法可以把其中一些插头变成另一种插头.求无法匹配插座的插头设备个数. 题解 用\(map\)给每个字符串标号为\(a_i\)和\(b_i\). 读入每种改变插头的方 ...
- 动态规划:HDU-2955-0-1背包问题:Robberies
解题心得: 这题涉及概率问题,所以要运用概率的知识进行解答.题目要求不被抓到的概率,但是给出的是被抓到的概率,所要用1减去后得到答案.最好使用double类型,避免精度问题导致WA. 先算出可以抢劫的 ...
- P3387 【模板】缩点
题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...
- 阿里云服务器+Tomcat项目+mysql 发布项目全过程
这个博客管理系统折腾我好几天了. 总结一下整个过程吧! 1.首先这个博客在tomcat下 windows系统可以完全跑起来了,无论是前台或者后台都能实现所有的功能. 2.然后我买了一个域名jasonj ...
- UVa 1455 Kingdom 线段树 并查集
题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...
- SpringMVC基本概念
DispatcherServlet:MVC的前端控制器,浏览器用户的请求经过DispatcherServlet的分发,到达合适的controller,生产业务数据所需要的model,model通过Di ...
- NOI p 2017 TG游记
嗨小朋友们大家好 还记得我是谁吗 对了我就是为iot配音的演员 弹鸡鸡 今天呐我特别的要向长沙市的oier们 洛谷的oier们 还有cnblogs的oier们问声好 为什么呢 因为我们在2017年11 ...
- [oldboy-django][6其他]navicat远程登录没有权限
day6-17-1204 # 增加远程访问mysql的权限(就是其他ip地址远程访问另外一个ip地址的数据库) -- step1 修改配置文件,bind_address, 允许所有ip地址都可以访问m ...