#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. 使用PHP开发一个简单的后台接口(响应移动端的get请求和post请求)

    写一个简单的后台,在接到app请求数据的时候,返回对应的内容: index.php文件如下: <?php $id = $_POST['id']; if($id==001){ echo json_ ...

  2. 【View层】界面绘制

    [引用]:http://www.jianshu.com/p/c5fc8c6b967a [View层]IOS纯代码绘制界面(一) 字数2303 阅读385 评论2 喜欢16 IOS开发中界面绘制占据了绝 ...

  3. Java链表的一些操作:

    [还有一些其他的算法提]http://blog.csdn.net/WalkingInTheWind/article/category/906980 [转]http://blog.csdn.net/lu ...

  4. 聊聊Node.js 独立日漏洞

    背景 Node.js 社区近期在美国独立日周末的狂欢之时爆出漏洞 https://medium.com/@iojs/important-security-upgrades-for-node-js-an ...

  5. partial类修饰符

    partial是一个类修饰符,用于把类定义拆分为几个部分,便于代码管理,如class ClassA{void A(){;}void B(){;}}与partial class ClassA{void ...

  6. Python3基础 filter与lambda表达式配合 筛选出1-100之间的奇数

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  7. 1.3.2. App Icon 和 Launch Image(Core Data 应用程序实践指南)

    App Icon: 选中 Assets.xcassets 选择 AppIcon ,并拖入图片(29.40.60) Launch Image: 创建 Launch Image 拖入图片(2x.R4)

  8. MySQL的"旁门左道"用法总结

    不断更新. 一.显示当前MySQL服务的版本:1是直接在查询窗口select version();2是show variables like 'version';

  9. 超强JavaScript编辑器WebStorm代码提示迟缓问题及其它想到的

    去D2后发现 Hedger 的js编辑器是webStorm,便下载来试试,结果发现: 虽然WebStorm的js的提示远远不如aptana那么强悍(输入字母d不提示document),但是做为 Int ...

  10. ui-router---$stateProvider

    转自:http://blog.csdn.net/violet_day/article/details/17515157 $stateProvider $stateProvider.state(stat ...