C++ 中各种map的使用

C++中有很多中key-value形式的容器,map/hash_map/unordered_map/vector_map。下面讲述各个map的使用及其区别。

首先,map的基本使用方法如下:

#include <iostream>
#include <map>
using namespace std;

typedef std::map<int, string> Map;
typedef Map::iterator MapIt;

int main()
{
	Map *map = new Map();
	int key;
	string value;
	while(cin>>key>>value)
	{
		map->insert(make_pair(key, value));
	}
	for(MapIt it = map->begin(); it != map->end(); ++it)
		cout<<"key:"<<it->first<<" value:"<<it->second<<endl;
	delete map;
	return 0;
}

map使用红黑树实现。查找时间在O(lg(n))-O(2*log(n))之间,构建map花费的时间比较长,因而,map使用于那种插入和查询混合的情况。如果是先插入后查询的情况,可以考虑使用vector_map.

vector_map在C++中没有实现,想使用可以自己实现。其基本思想在于使用vector来保存数据,插入完成后进行排序,然后使用而分查找进行查询。这样在先插入后查询的条件下,性能会比map好很多。原因主要在一下几个方面。

  1. vector使用线性存储,map是二叉树形状,所以vector的局部性更好。
  2. vector可以一次分配很大的内存,而map需要每次分配一个节点,而且map中相对于vector有很多冗余数据,比如指向子节点的指针。
  3. vector是插入完成后统一进行排序,而map每次insert都有一次查找和树的旋转。
  4. vector_map是二分查找,查找时间稳定在O(lg(n)),而map的存储结构是红黑树,查找时间为O(lg(n))-O(2*log(n))。

map的key可以是自定义数据结构,但是需要重载<运算符。如下代码所示:

typedef struct _Key
{
	_Key(int *p, int l)
	{
		len_ = l;
		for(int i = 0; i < l; ++i)
			p_[i] = p[i];
	}
	bool operator<(const _Key &rs) const
	{
		if(len_ == rs.len_)
		{
			for(int i = 0; i < len_; ++i)
				return p_[i] < rs.p_[i];
			return false;
		}
		else
			return len_ < rs.len_;
	}
	int p_[MaxLen];
	int len_;
}Key;
typedef std::map<Key, vector<int> *> MyMap;

需要注意的是,重载函数必须为const的。

当然,你也可以这么做:

typedef struct _Key
{
	_Key(int *p, int l)
	{
		len_ = l;
		for(int i = 0; i < l; ++i)
			p_[i] = p[i];
	}
	int p_[MaxLen];
	int len_;
}Key;
typedef struct _KeyCmp
{
	bool operator()(const Key &ls, const Key &rs)
	{
		if(ls.len_ == rs.len_)
		{
			for(int i = 0; i < ls.len_; ++i)
				return ls.p_[i] < rs.p_[i];
			return false;
		}
		else
			return ls.len_ < rs.len_;
	}
}KeyCmp;
typedef std::map<Key, vector<int> *, KeyCmp> MyMap;

与上面有相同的效果。

hash_map,STL中的实现叫做unordered_map,都是基于hash_table实现的。首先,分配一大片内存,形成很多桶。利用hash函数,将key映射到不同的桶中,当然,也有可能会有两个不同的key映射到同一个桶中,这是,就需要判别函数来进行查找了。所以,hash_map的key需要两个条件,一个是hash函数,获得映射到的桶的值,另外一个是equal_to函数,判定两个key是否相等。显然,当每个桶里的元素个数比较平均且比较少的时候,查询性能比较高。

使用样例如下:

#include <string>
#include <iostream>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;

struct str_hash
{
	size_t operator()(const string &s) const
	{
		return __stl_hash_string(s.c_str());
	}
};

struct str_compare
{
	int operator()(const string &a, const string &b) const
	{
		return (a==b);
	}
};
typedef hash_map<string, string, str_hash, str_compare> StrMap;

int main()
{
	StrMap strMap;
	string a,b;
	cout<<"插入:"<<endl;
	while(cin>>a>>b)
	{
		if(a.length() <= 1)
			break;
		strMap.insert(make_pair(a,b));
	}
	cout<<"查询:"<<endl;
	while(cin>>a)
	{
		if(a.length() <= 1)
			break;
		if(strMap.find(a) != strMap.end())
			cout<<strMap[a]<<endl;
		else
			cout<<"not found"<<endl;
	}
	return 0;
}

在编译的时候会遇到warning:

***@ubuntu:~/Maps$ g++ -o hm hash_map.cpp 
In file included from /usr/include/c++/4.6/ext/hash_map:61:0,
                 from hash_map.cpp:3:
/usr/include/c++/4.6/backward/backward_warning.h:33:2: 警告: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]

按照提示,g++编译时添加参数即可消除。

unordered_map和hash_map的使用方式差不多,如下所示:

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

unsigned int JSHash(const char *str)
{
	unsigned int hash = 1315423911;
	while(*str)
	{
		hash ^= ((hash<< 5) + (*str++) + (hash>>2));
	}
	return (hash & 0x7FFFFFFF);
}

struct StrHash
{
	size_t operator()(const string &s) const
	{
		return JSHash(s.c_str());
	}
};
struct StrCompare
{
	bool operator()(const string &a, const string &b) const
	{
		return a==b;
	}
};
typedef unordered_map<string, string, StrHash, StrCompare> MyMap;
int main()
{
	MyMap mymap;
	string a,b;
	while(cin>>a>>b)
	{
		mymap[a] = b;
	}
	for(MyMap::iterator it = mymap.begin(); it != mymap.end(); ++it)
		cout<<it->first<<" "<<it->second<<endl;
	return 0;
}

如果直接g++不带其他参数编译的话,会提示错误:

***@ubuntu:~/Maps$ g++ -o um unordered_map.cpp 
In file included from /usr/include/c++/4.6/unordered_map:35:0,
                 from unordered_map.cpp:2:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: 错误: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
unordered_map.cpp:30:9: 错误: ‘unordered_map’不是一个类型名
unordered_map.cpp: 在函数‘int main()’中:
unordered_map.cpp:33:2: 错误: ‘MyMap’在此作用域中尚未声明
unordered_map.cpp:33:8: 错误: expected ‘;’ before ‘mymap’
unordered_map.cpp:37:3: 错误: ‘mymap’在此作用域中尚未声明
unordered_map.cpp:39:6: 错误: ‘MyMap’既不是类也不是命名空间
unordered_map.cpp:39:22: 错误: expected ‘;’ before ‘it’
unordered_map.cpp:39:42: 错误: ‘it’在此作用域中尚未声明
unordered_map.cpp:39:48: 错误: ‘mymap’在此作用域中尚未声明

需要在编译时添加-std=c++0x参数即可。

总体来说,hash_map的查找速度比map要快,因为hash_map的查找速度与数据量大小无关,属于常数级别。map的查找速度是log(n)级别。但是hash_map每次查找都需要执行hash函数,所以也比较耗时。而且,hash_map很多桶中可能都没有元素,所以内存利用率不高。

所以,选择map的时候,需要从三个方面考虑:应用场景/内存占用/查找速度。

本次总结到此完毕,如有不详尽之处或错误,请多多指教。


参考链接:

http://www.cnblogs.com/Frandy/archive/2011/07/26/Hash_map_Unordered_map.html

http://yujiawei.iteye.com/blog/409774

http://www.189works.com/article-7126-1.html

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值