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中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处 ...
随机推荐
- java实现——003二维数组中的查找
import java.util.Scanner; public class T003 { public static void main(String[] args) { Scanner in = ...
- WebForm和MVC中都可以使用的路由
1.在global.asax void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 // RouteConfig. ...
- .NET防止重复提交数据
最近在做一个销售系统的时候,操作人员提交数据的时候数据库竟然会出现多条数据相同的记录,并且是在1秒之内,客户反馈给我们,第一反应是重复提交的问题,检查了下代码,程序执行完成后应该是跳转到别的页面的,可 ...
- 兼容ie6及以上和firefox等标准浏览器的表格行滑过时背景色切换的效果
一.js代码——"tablehover.js" /** *②.表格单元行滑过时高亮样式动效组件封装 *oop形式封装交互动效类 *组件说明这个组件是为 ...
- Java 读写Properties配置文件
Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...
- Angular Cookies 操作
$cookiesProvider 使用$cookiesProvider改变$cookies服务的默认行为. 默认属性 path:字符串,cookies只在这个路径及其子路径可用.默认情况下,这个将会是 ...
- 响应的系统设置的事件——Configuration类简介
Configuration类专门用于描述手机设备上的配置信息,这些配置信息既包括用户特定的配置项,也包括系统的动态设置配置. 程序可调用Activity的如下方法来获取系统的Configuration ...
- Unable to resolve target 'android-XX'解决办法
在搭建好安卓编译环境后,我用Eclipse导入冲git上下载的安卓源码编译时,会提示 Unable to resolve target 'android-17' 等 “Unable to resolv ...
- C# 程序集 和 反射
.Net反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为.Net的反射机 ...
- JSP编译为Java类
JSP编译为Java类: 注意可以随便写import的内容:可以写类属性.方法.main函数.内部类:可以使用内部类: JSP: <%@ page language="java&quo ...