#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 的思考的更多相关文章

  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. 分享 php array_column 函数 无法在低版本支持的 修改

    function i_array_column($input, $columnKey, $indexKey=null){ if(!function_exists('array_column')){ $ ...

  2. JQ剪辑图片插件,适用于移动端和PC端

    主要用到以下JS文件: <script src="js/photo/iscroll-zoom.js"></script> <script src=&q ...

  3. 11,scrapy框架持久化存储

    今日总结 基于终端指令的持久化存储 基于管道的持久化存储 今日详情 1.基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的 ...

  4. Android开发——用户在屏幕上的手势识别

    个定点决定.四个属性分别为left(1),top(2),right(3),bottom(4). 数字为图上标出的距离.显然这四个属性是相对于父容器来定的,均可以通过get()方法获取. 因此很容易得出 ...

  5. P2920 [USACO08NOV]时间管理Time Management

    P2920 [USACO08NOV]时间管理Time Management 题目描述 Ever the maturing businessman, Farmer John realizes that ...

  6. sqoop安装和使用

    下载版本:sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz 官网:http://mirror.bit.edu.cn/apache/sqoop/1.4.6/ jdbc ...

  7. day 16 JS DOM 继续

    为什么有jquey了还学DOM  ? 因为JQuey 是大而全,可能有10k 但是我们用到的只有1k  网站小没事,网站大流量就是问题 所以大网站都是自己用DOM 实现一个类似于JQuey 的适合自己 ...

  8. loj2308 「APIO2017」商旅

    ref #include <iostream> #include <cstring> #include <cstdio> #include <queue> ...

  9. python - 接口自动化测试 - TestRegister - 注册接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_register.py @ide: PyChar ...

  10. 精通CSS高级Web标准解决方案(4、对链接应用样式)

    4.1 简单的链接样式 锚可以作为内部引用,也可以作为外部链接,应该区分对待. 伪类选择器: :link 用来寻找没有访问过的链接 :visited 用来寻找已经访问过的链接 a:link{color ...