两者效率对比:

#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. 浅析初等贪吃蛇AI算法

    作为小学期程序设计训练大作业的一部分,也是自己之前思考过的一个问题,终于利用小学期完成了贪吃蛇AI的一次尝试,下作一总结. 背景介绍: 首先,我针对贪吃蛇AI这一关键词在百度和google上尽心了检索 ...

  2. Objective-C 再谈OC指针,对比C++/Java/Swift

    1.Objective-C的指针 OC一直是人感觉比较变态的一门语言,为什么呢?因为它的每个变量都是指针型,多的都几乎让人忘了那个*的存在了. 比如我定义了一个Student的Class,new了st ...

  3. sed命令给文本文件的每行的行首或者行尾添加文字

    在每行的头添加字符,比如"HEAD",命令如下: sed 's/^/HEAD&/g' test.file 在每行的行尾添加字符,比如“TAIL”,命令如下: sed 's/ ...

  4. HDU 5923 Prediction

    这题是2016 CCPC 东北四省赛的B题, 其实很简单. 现场想到的就是正解, 只是在合并两个并查集这个问题上没想清楚. 做法 并查集合并 + 归并 对每个节点 \(u\), 将 \(u\) 到根的 ...

  5. Handler,Thread,Looper之间关系小结

    http://blog.csdn.net/sunxingzhesunjinbiao/article/details/6794840 (1) Looper类别用来为一个线程开启一个消息循环.默认情况下A ...

  6. Tmux

    常用按键 这里需要说明一点的是,tmux的任何指令,都包含一个前缀,也就是说,你按了前缀(一组按键,默认是Ctrl+b)以后,系统才知道你接下来的指令是发送给tmux的. C-b ? 显示快捷键帮助 ...

  7. JavaScript函数之美~

    JavaScript函数之美~ 这篇文章,我将就以下几个方面来认识JavaScript中的函数. 函数为什么是对象,如何定义函数? 如何理解函数可以作为值被传递 函数的内部对象.方法以及属性 第一部分 ...

  8. Yocto开发笔记之《网卡配置》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ============================================== # ifconfig -a # ...

  9. [转发]Dumps of system information with Apple computers

    In this article, I gathered up all the dumps, who found. If you see something new table will be upda ...

  10. jQuery实现表格行的动态增加与删除 序号 从 1开始排列

    <table id="tab" border="1" width="60%" align="center" sty ...