map与hash_map使用与对比
#include <iostream>
#include <functional>
#include <map>
#include <ext/hash_map>
#include <string>
using std::string; struct SInfo
{
string id_;
string name_;
SInfo(string id, string name):id_(id),name_(name){}
}; // map example
template<class T1>
struct SLessThan : public std::binary_function<T1, T1, bool>
{
bool operator()(const T1 &first, const T1 &second) const
{
return first.id_ < second.id_;
}
}; void testmap()
{
std::cout<<"------------------- test map ----------------------------"<<std::endl;
typedef std::map<SInfo, string, SLessThan<SInfo> > Info2IDMap;
Info2IDMap info2IDMap;
info2IDMap.insert(std::make_pair(SInfo("", "zhangsan"), ""));
info2IDMap.insert(std::make_pair(SInfo("", "lisi"), ""));
info2IDMap[SInfo("", "wanger")] = ""; for(Info2IDMap::iterator iter = info2IDMap.begin(); iter != info2IDMap.end(); ++iter)
{
std::cout<<iter->second<<";";
}
std::cout<<std::endl;
} // hash_map example
typedef struct SHashInfo
{
size_t operator()(const SInfo &info) const
{
unsigned long __h = ;
for (size_t i = ; i < info.id_.size() ; i ++)
__h = *__h + info.id_[i];
return size_t(__h);
}
}SHashInfo; template<class T1>
struct SEqual : public std::unary_function<T1, bool>
{
bool operator()(const T1 &first, const T1 &second) const
{
return first.id_ == second.id_;
}
}; void testhashmap()
{
std::cout<<"-------------------- test hash map -----------------------------"<<std::endl;
typedef __gnu_cxx::hash_map< SInfo, string, SHashInfo, SEqual<SInfo> > Info2IDMap;
Info2IDMap info2IDMap();
info2IDMap[SInfo("", "beijing")] = "";
info2IDMap[SInfo("", "shanghai")] = "";
info2IDMap[SInfo("", "tianjin")] = ""; for(Info2IDMap::iterator iter = info2IDMap.begin(); iter != info2IDMap.end(); ++iter)
{
std::cout<<iter->second<<";"<<std::endl;
}
std::cout<<std::endl;
} int main(int argc, char *argv[])
{
testmap();
testhashmap(); return ;
}
通过上面的例子会发现:
1. map需要指定小于函数(可使用默认配置)。
2. hash_map需要指定哈希函数和等于函数。其中针对普通类型有通用配置。
3. hash_map还未被列入标准库中。
4. map的底层使用的是红黑树,因此可以保证数据有序,而hash_map的底层使用的是哈希表,不能保证数据有序。
疑问:map和hash_map的使用如何选择?
当数据量很大且hash冲突比较小,数据增删比较少,关心查询性能时使用hash_map;
增删数据较多的情况下使用map。
具体使用还需要根据具体场景判断,主要考虑:数据量,查询速度,内存占用(要注意对象构造速度,hash_map的慢)。
map与hash_map使用与对比的更多相关文章
- map,hash_map, hash_table, 红黑树 的原理和使用
在刷算法题的时候总是碰到好多题,号称可以用hash table来解题.然后就蒙圈了. 1.首先,map和hash_map的区别和使用: (1)map底层用红黑树实现,hash_map底层用hash_t ...
- Map和hash_map
map和hash_map 今天在写拼流的程序时碰到一个问题,要根据流的四元组的结构信息映射到该流的数据.也就是我在网络数据包拼接的过程中,要根据包的地址和端口信息,对应到其对应的一个流的数据上去,把端 ...
- STL中map与hash_map容器的选择收藏
这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...
- STL 中的map 与 hash_map的理解
可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...
- STL中的map和hash_map
以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...
- map、hash_map、unordered_map 的思考
#include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...
- Set,Map与Array,Object对比
Map与Array 数据结构横向对比,用Map和Array分别实现最基本的增删改查: //增 { let theMap=new Map(); let theArray=[]; theMap.set(' ...
- map vs hash_map
1. map, multimap, set, multiset g++ 中 map, multimap, set, multiset 由红黑树实现 map: bits/stl_map.h multim ...
- map,hash_map和unordered_map 实现比较
map介绍 Map是STL[1]的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处 ...
随机推荐
- ASP.NET常用的控件怎么添加
http://jingyan.baidu.com/article/d8072ac4224747ec95cefda2.html
- 三 APPIUM Android自动化 测试初体验
1.创建一个maven项目 成功新建工程: 编辑pom.xml,在<dependencies></dependencies>下添加appium相关依赖: <depende ...
- error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches
error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches &quo ...
- js原生之设计模式开篇介绍
本文主要讲述一下,什么是设计模式(Design pattern),作为敲键盘的我们要如何学习设计模式.设计模式真的是一把万能钥匙么? 各个代码的设计模式几乎每个人都知晓,就算不会那也一定在一些 ...
- easyui弹出窗关闭前调用确认窗口,先关闭页面后调用弹出窗口
弹出窗关闭的时候提示是否关闭,同时进行一些对应的方法调用, 然而在进行页面关闭调用的时候,往往页面关闭了,才弹出确认对话框, $.messager.confirm和panel的onBeforeClos ...
- C语言中strcpy,strcmp,strlen,strcat函数原型
//strcat(dest,src)把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0' char *strcat(char * strDest, const char ...
- july教你如何迅速秒杀掉:99%的海量数据处理面试题
作者:July出处:结构之法算法之道blog 以下是原博客链接网址 http://blog.csdn.net/v_july_v/article/details/7382693 微软面试100题系列 h ...
- 获取FMS的状态信息
application.getStats() application.getStats() Returns statistics about an application. Returns An Ob ...
- [心得]传统IT转互联网面试经验分享
http://www.newsmth.net/bbstcon.php?board=Java&gid=374779 传统IT外企干了8年,两年前转互联网的,面的和被面的都不少.这几天项目空档期, ...
- HoloLens开发手记 - 构建2D应用 Building 2D apps
HoloLens可以让我们在真实世界中看到全息图像内容.但是它本质上还是一台Windows 10设备,这意味着HoloLens可以以2D应用形式运行Windows Store里的大部分UWP应用. 目 ...