#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使用与对比的更多相关文章

  1. map,hash_map, hash_table, 红黑树 的原理和使用

    在刷算法题的时候总是碰到好多题,号称可以用hash table来解题.然后就蒙圈了. 1.首先,map和hash_map的区别和使用: (1)map底层用红黑树实现,hash_map底层用hash_t ...

  2. Map和hash_map

    map和hash_map 今天在写拼流的程序时碰到一个问题,要根据流的四元组的结构信息映射到该流的数据.也就是我在网络数据包拼接的过程中,要根据包的地址和端口信息,对应到其对应的一个流的数据上去,把端 ...

  3. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...

  4. STL 中的map 与 hash_map的理解

    可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...

  5. STL中的map和hash_map

    以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...

  6. map、hash_map、unordered_map 的思考

    #include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...

  7. Set,Map与Array,Object对比

    Map与Array 数据结构横向对比,用Map和Array分别实现最基本的增删改查: //增 { let theMap=new Map(); let theArray=[]; theMap.set(' ...

  8. map vs hash_map

    1. map, multimap, set, multiset g++ 中 map, multimap, set, multiset 由红黑树实现 map: bits/stl_map.h multim ...

  9. map,hash_map和unordered_map 实现比较

    map介绍 Map是STL[1]的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处 ...

随机推荐

  1. svn无法提交

    svn无法提交, 错误信息:Commit failed. svn: E200007: CHECKOUT can only be performed on a version resource... 解 ...

  2. Django performance

    Reference: https://impythonist.wordpress.com/2016/02/21/building-high-performance-django-systems/ Th ...

  3. 【Xilinx-Petalinux学习】-08-OpenAMP系统实现

    openAMP系统实现. 一个核跑Linux,一个核裸跑.     ----->  已经实现. 一个核跑Linux,一个核跑UCOS   ----->  还未实现. Micrium的uco ...

  4. Hibernate的一些事儿

    一.Hibernate的工作原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6.提 ...

  5. 闭包用法,延迟tab

    var changeTab =( function () { var timeId = 0; return function (tabId) { if (timeId) { clearTimeout( ...

  6. centos 修改/etc/fstab后无法启动

    今天做实验,增加了一个磁盘sdb1,而且也增加了自动挂载的功能/etc/fstab里增加了记录. 重新启动服务器的时候,系统启动不了了. 系统提示: 按提示 输入 root的密码,进入以Repair ...

  7. 分布式cookie-session的实现(spring-session)

    分布式cookie-session的实现(spring-session) 本文使用的spring-session版本为 1.0.0,地址为: https://github.com/spring-pro ...

  8. AdapterViewFlipper的功能和用法

    AdapterView继承了AdapterViewAnimator,它也会显示Adapter提供的多个View组件,但每次只能显示一个View组件,程序可通过showPrevious和showNext ...

  9. Windows Server 2008 R2防火墙出站规则

    出战规则指Windows Server 2008 R2系统访问外部的某台计算机通信数据流. 配置防火墙阻止Windows Server 2008 R2系统通过IE软件访问外部的网站服务器,阻止Wind ...

  10. WInform 创建一个简单的WPF应用

    (一)创建一个简单的WPF应用 首先,在这里我要说明的是:这里的例子,都是通过控制台程序来创建WPF应用,而非使用现成的WPF模版.因为WPF模版封装了创建WPF应用所需要的各种基本元素,并不利于我们 ...