map 与 unordered_map
两者效率对比:
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
#include <sys/time.h>
#include <list> using namespace std; template<class T>
void fun(const T& t, int sum)
{
for(int i = ; i < sum; i++)
t.find(i);
} template<template <class...> class T>
float init(int sum)
{
T<int,int> t;
for(int i = ; i < sum; i++)
t.insert(make_pair(i,i));
struct timeval begin,end;
gettimeofday(&begin,NULL);
fun(t,sum);
gettimeofday(&end,NULL);
float time = end.tv_sec-begin.tv_sec + float(end.tv_usec-begin.tv_usec)/;
cout<<"\033[32msum: "<<sum<<"\ttime: "<< time <<" sec"<<endl;
return time;
} int main(int argc,char** argv)
{
list<int> lt;
for(int i = ; i <= ; i*=)
{
lt.push_back(i);
}
for(auto& item : lt)
std::cout<<"\033[31m"<<init<unordered_map>(item)/init<map>(item) <<"\033[0m\n-------------------\n"<<std::endl;
}
本机测试结果为(Intel(R) Xeon(R) CPU E5620 @ 2.40GHz):

例子:
#include <iostream>
#include <string>
#include <unordered_map>
#include <map> struct Test
{
int gameid;
int subgameid;
int roomid; //实现 map 的键条件:
bool operator<(const Test& obj) const
{
if(gameid < obj.gameid)
return true;
else if(gameid == obj.gameid && subgameid < obj.subgameid)
return true;
else if(gameid == obj.gameid && subgameid == obj.subgameid && roomid < obj.roomid)
return true;
else
return false;
} //实现 unordered_map 的键条件之一,还有一条件为实现hash算法
bool operator==(const Test& obj) const
{
return gameid == obj.gameid && subgameid == subgameid && roomid == obj.roomid;
} }; //第一种方法:
namespace std
{
template <typename T>
class hash
{
public:
long operator()(const T& o) const { return ; }
}; template <> class hash<Test>
{
public:
long operator()(const Test& x) const
{
return x.gameid* + x.subgameid* + x.gameid;
}
};
} //第二种方法
class test_hash
{
public:
long operator()(const Test& x) const
{
return x.gameid* + x.subgameid* + x.gameid;
}
}; int main()
{
std::unordered_map<Test,int> m1;
std::unordered_map<Test,int,test_hash> m2;
std::map<Test,int> m;
Test test1{,,};
Test test2{,,};
Test test3{,,};
m.insert(std::make_pair(test1,));
m.insert(std::make_pair(test2,));
Test test4{,,};
std::cout<<m.size()<<std::endl;
std::cout<<m[test4]<<std::endl;
}
map 与 unordered_map的更多相关文章
- 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) 首先 ...
随机推荐
- VMware“该虚拟机似乎正在使用中”问题
在用VMware虚拟机的时候,有时会发现打开虚拟机时提示“该虚拟机似乎正在使用中.如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权.否则,请按“取消(C)”按钮以防损坏.配置文件: D ...
- 获取exe目录
System.IO.Directory.GetCurrentDirectory()这个方法,会随着你的当前系统路径的改变而改变.比如你打开一个openFileDialog那么,再次获得路径就不对了.s ...
- appium+java+junit demo运行
对java熟悉一些,所以想用java把appium给做起来.今天用myeclipse给环境和Demo弄了一下,网上没有一篇全套资料的. 各块环境的搭建: 1.安装appium客户端,省略下载和安装步骤 ...
- Alpha版本十天冲刺——Day 7
站立式会议 祝曹鑫杰和常松童鞋生日快乐!短短几天冲刺,就迎来了三位队员的生日,希望也给我们的Alpha版本带来好运,加油! 会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 上传图片接口 无 ...
- 常见linux命令释义(第二天)
查看磁盘与目录的容量. df 与du df 是英语Disk Free的意思.磁盘空余. df -h -h中h指的是一human(人类)的角度,即用GB,MB的格式显示.这个比较常用,还有其他的选项 ...
- SPOJ:ABCDEF
传送门 废话不说,这道题暴力枚举是$O(N^6)$,显然无法承受. 推导一下 $(x_1*x_2+x_3)/x_4-x_5=x_6$ $x_1*x_2+x_3=x_4*(x_5+x_6)$ 等式左边和 ...
- ionic 发送请求返回一直都是404
在web端调试一直都没有问题,生成app之后发现所有的请求返回的都是404, 断掉wifi和4G之后发送的也是404 原因是未引入插件 解决方法是在根目录下执行一下命令 cordova plugin ...
- mysql 数据库怎样快速的复制表以及表中的数据
1.copy表结构 SHOW CREATE TABLE runoob_tbl:(选择你要复制的表),copy 它的创建语句,执行即可创建表结构相同的表了,记得改名称哦. 2.copy表数据 选择你要插 ...
- WinForm------TreeList实现鼠标经过节点背景色改变
转载: http://www.cnblogs.com/zfanlong1314/archive/2012/06/26/2564124.html
- 提取hdfs文件名的方法
#!/bin/bash #------------------------------------------------------------- # 把目录中的所有文件getmerge到本地 # ...