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) 首先 ...
随机推荐
- 算法训练 Eurodiffusion
Eurodiffusion /***********并未完全AC***********/ #include<iostream> #include<algorithm> #inc ...
- 二分法:CF371C-Hamburgers(二分法+字符串的处理)
Hamburgers Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Desc ...
- git 使用规范
git使用资料: https://github.com/peak-c/my-git 公司内部使用开发规范: 一. 代码库介绍 个人开发库(git@gitlab.adrd.sohuno.com:sper ...
- #3 working with data stored in files && securing your application
This chapter reveals that you can use files and databases together to build PHP application that waa ...
- 4152: [AMPPZ2014]The Captain
4152: [AMPPZ2014]The Captain Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1561 Solved: 620[Submi ...
- P1616 疯狂的采药
P1616 疯狂的采药 题目背景 此题为NOIP2005普及组第三题的疯狂版. 此题为纪念LiYuxiang而生. 题目描述 LiYuxiang是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为 ...
- windows下git安装以及基本配置
一.首先要下载git然后进行默认安装即可,然后查看系统环境变量是否添加 二.桌面右击鼠标,选择git bash 进入操作页面 git --version 检测是否安装成功 安装完成有配置一 ...
- 我给女朋友讲编程CSS系列(3) CSS如何设置字体的类型、大小、颜色,如何使用火狐浏览器的Firebug插件查看网页的字体
一.CSS如何设置字体的类型.大小.颜色 设计网页时,一般设置body的字体,让其他标签继承body的字体,这样设置特别方便,但是标题标签h1到h6和表单标签(input类型)是没有继承body的字体 ...
- MongoDB快速入门学习笔记4 MongoDB的文档查询操作
先把student删除,再重新插入数据 > db.student.drop() true > db.student.insert([{ "_id" : 1, " ...
- 整理的一些Android开发类免费视频课程
1.Android实战淘宝网项目视频:http://edu.ibeifeng.com/view-index-id-248.html 2.Android滚动视差实现课程:http://edu.ibeif ...