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) 首先 ...
随机推荐
- VS联调多个解决方案的项目
一.项目中经常出现一个解决方案里面有多个程序,如果想按F5启动多个实例进行操作调试那该怎么操作? 以前自己都使用附加进程的方法调试,这样的调试不需要按F5,自己只要运行多个程序后,使用vs的附加进程到 ...
- ZendGuardLoader安装
遇到的问题 php -v Zend Guard Loader requires Zend Engine API version 220090626. The Zend Engine API versi ...
- dedecms \plus\guestbook.php SQL Injection Vul By \plus\guestbook\edit.inc.php
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 注射漏洞成功需要条件如下 . php magic_quotes_gpc= ...
- js调用刷新
_self.fireEvent('refresh');
- String、StringBuffer、StringBuilder源码分析
利用反编译具体看看"+"的过程 1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 int ...
- MOOCULUS微积分-2: 数列与级数学习笔记 2. Series
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
- Centos下查看占用端口并关闭进程方法
1.查看端口占用情况:netstat –tlnp (加p可以看到是哪个进程占用了端口); 也可以用grep查找对应的被占用的端口,键入netstat –tlnp | grep 3306可以看到PI ...
- 屠蛟之路_集木成舟_ForthDay
下数据库大山,行数里至水岸,无边无际的东海便豁然展现在屠蛟少年的眼前. 要想到达东海之中的蛟灵岛绞杀beta怪蛟,夺回心爱的小公举,少年们首先需要一艘经得起风浪的船.毕竟海上之路暗涌潜伏.同样凶险万分 ...
- Docker入门教程(三)Dockerfile
Docker入门教程(三)Dockerfile [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第三篇,介绍了Dockerfile的语法,DockerOn ...
- JDBC编程的方式
JDBC编程的方式,我们以一个简单的查询为例,使用JDBC编程,如下: 从上面可以看出JDBC编程一般要如下步骤: 1. 加载数据库驱动 2. 创建并获取数据库连接 3. 创建jdbc stateme ...