map和multimap, hash_map,和hash_multimap

#include<hash_map>和#include的操作是类似的。不同的是他们的底层数据结构是不同的。hash_map的底层数据结构是hash_table 数据库,而map的底层数据结构是红黑树。
hash_table 的插入和查询速度是O(1),而map的查询速度是log(n)。但这并不一定表示hash_map比map优秀,因为hash_map是无序表,而map是排序好的。另外hash_map访问是需要运行一次hash function, hash_map的读取速度一定比map快。
hash_map与map 类似可以插入多个相同键值,但只能访问一个键值。
hash_multimap和multimap可以实现插入多个键值,访问多个键值。
下面我们贴出一段代码

	int main(){
		hash_multimap<int,int> hm;
		hm.insert(pair<int,int>(1,10));
		hm.insert(pair<int,int>(2,20));
		hm.insert(pair<int,int>(2,21));
		hm.insert(pair<int,int>(3,30));
		hm.insert(pair<int,int>(4,40));
		hm.insert(pair<int,int>(2,22));
		hm.insert(pair<int,int>(5,50));

		hash_multimap<int,int>::iterator beg = hm.begin(), end = hm.end(),ite;

		for(ite = beg; ite != end; ite++)
			cout << "<" << ite->first << "," << ite->second << ">" << " ";
		cout << endl;

		hash_multimap<int,int>::iterator beginner, ender, mapIter;
		beginner = hm.lower_bound (2);
		ender = hm.upper_bound(2);
		for(mapIter = beginner; mapIter != ender; mapIter++) {
			cout << "<" <<mapIter->first << "," << mapIter->second << ">" << " ";
		}
		system("pause");
		return 0;
}

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值