两者效率对比:

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

  1. map和unordered_map的差别和使用

    map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/billcyj/article/details/7 ...

  2. 【转】Map 与 Unordered_map

    map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...

  3. C++ map与unordered_map

    map与unordered_map对比 map unordered_map 红黑树(非严格二叉平衡搜索树)实现 哈希表实现 有序 无序 -- 查找时间复杂度为O(1),非常快 空间消耗较大 空间消耗较 ...

  4. STL中的map和unordered_map

    STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...

  5. C++中map和unordered_map的用法

    1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...

  6. map和unordered_map使用小结

    map和unordered_map unordered_map简介: #include <cstdio> #include <iostream> #include <un ...

  7. 原 c++中map与unordered_map的区别

    c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...

  8. 关于c++ STL map 和 unordered_map 的效率的对比测试

    本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...

  9. Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序

    写程序时,面临用Map还是unordered_map,总是很纠结,于是写了个程序进行测试 Map 与 unordered_map 横向与纵向测试,附带原始数据与测试程序 简单数据(4 Byte) 首先 ...

随机推荐

  1. 【bzoj3218】 a + b Problem

    http://www.lydsy.com/JudgeOnline/problem.php?id=3218 (题目链接) 题意 给${n}$个格子涂白或黑色,白则${w_i}$,黑则${b_i}$的好看 ...

  2. .net读写config appsetting

    读 this.txtOutPutPath.Text = ConfigurationManager.AppSettings["OutPutPath"]; this.txtFilter ...

  3. 针对CMS中的tag标签理解

    针对CMS的tag标签有以下解释: 什么tag标签? TAG标签是一种由自定义的一种标签,要比分类更加的准确,可以概括文章主要内容的关键词. 运用TAG标签,可以使网站的文章更容易被搜索引擎检索到.百 ...

  4. CruiseControl.NET与TFS结合的配置文件

    配置如下: <cruisecontrol xmlns:cb="urn:ccnet.config.builder"> <project name="测试项 ...

  5. MVC5-3 Result分析

    众多的Result 使用MVC进行开发,可以看到有ActionResult.ContentReuslt.JsonResult..等,今天对这些Result进行背后分析.它到底是如何做到的 Action ...

  6. AngularJs $cacheFactory 缓存服务

    可能之前的api写的有些枯燥吧,因为不烧脑,不需要很多逻辑思维来做处理,那么之后的文章会有趣很多,慢慢的开始烧脑了,准备好大量脑细胞的死亡吧~   先来篇简单的缓存服务. 本文将api文档里的$cac ...

  7. django redis操作

    from utils.redis.connect import redis_cache as rr.flushdb() 列表操作 r.lpush("name", xxxx) or ...

  8. CentOS 下安装

    2016年12月5日15:25:58 ----------------------------------- 通常情况下在centos下安装软件就用yum. 关键是,使用yum你要知道安装包的名字是什 ...

  9. iOS - CALayer相关(CATransform3D)

    一.图层的几何 图层的几何简单通俗,图层的所有几何属性(包括矩阵变换),都可以有隐式和显式动画. 图层几何的属性: 1.position是CGPoint值,她指定图层相对于她图层的位置,该值基于父图层 ...

  10. 使用LaTeX编辑数学公式

    首先在博客园的页首html里添加以下代码: <script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex ...