#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. List<KeyValuePair<TKey,TValue>> 与 Dictionary<TKey,TValue> 不同

    两者都可以通过 KeyValuePair<TKey,TValue> 进行遍历,并且两者可以相互转换: List<KeyValuePair<string,string>&g ...

  2. scrollview嵌套gridview滑动问题

    在开发过程总遇到ScrollView嵌套GridView,由于这两种控件都带有滚动条,当他们碰到一起的时候便会出问题,问题是gridview不滚动,并且只显示两行,为此看了官方文档,谷歌回答滚动里面没 ...

  3. 文件查找和比较命令 来自: http://man.linuxde.net/find

    文件查找和比较1.find命令,用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时不设置任何参数,则find命令则在当前目录下查找子目录与文件.并且将查到的子 ...

  4. angularjs---服务(service / factory / provider)

    初angularJs时  常写一些不够优雅的代码  !我总结了一下看看各位有没有中枪的!-----( 这里只针对服务service及其相关! ) 以下做法不太优雅 兄弟controller 之间的相同 ...

  5. centos5.5get 递归下载整个网站

    这个命令可以以递归的方式下载整站,并可以将下载的页面中的链接转换为本地链接. wget加上参数之后,即可成为相当强大的下载工具. wget -r -p -np -k http://xxx.com/xx ...

  6. 【翻译】使用Visual Studio创建Asp.Net Core MVC (一)

    This tutorial will teach you the basics of building an ASP.NET Core MVC web app using Visual Studio ...

  7. js dorado

    1.dorado http://bsdn.org/projects/dorado7/deploy/sample-center/com.bstek.dorado.sample.Main.d#40350

  8. JWPlayer使用指南

    http://support.jwplayer.com/customer/portal/articles/1499103-quickstart-reference <div id="m ...

  9. 如何使用DockerHub官方的mysql镜像

    Mysql是一个广泛使用的开源关系型数据库. 如何获取Mysql Docker镜像? docker pull mysql:5.7 如何使用这个Docker镜像? 1.启动一个Mysql Server容 ...

  10. Hybrid容器设计之第三方网站

    平台化容器API释放 接上文:(阅读本文前,建议阅读前三篇文章先) 浅谈Hybrid技术的设计与实现 浅谈Hybrid技术的设计与实现第二弹 浅谈Hybrid技术的设计与实现第三弹——落地篇 之前设计 ...