std::hash_map和stdext::hash_map和std::map的基本插入用法

#include <windows.h>
#include <hash_map>
#include <map>

using namespace std;
using namespace stdext;

typedef std::hash_map<int, string>				_hash_map;
typedef std::pair<int, string>					_hash_map_pair;
typedef std::hash_map<int, string>::iterator	_hash_map_iterator;

typedef std::map<int, string>					_map;
typedef std::map<int, string>::iterator			_map_iterator;

int _tmain(int argc, _TCHAR* argv[])
{
	//_hash_map tmpMap;

	//tmpMap[1] = string("1");
	//tmpMap[1] = string("2");
	//tmpMap[1] = string("3");

	//tmpMap.insert(_pair(1, string("1")));
	//tmpMap.insert(_pair(1, string("2")));
	//tmpMap.insert(_pair(1, string("3")));

	//
	_map tmpMap;

	tmpMap.insert(_hash_map_pair(1, string("1")));
	tmpMap.insert(_hash_map_pair(1, string("2")));
	tmpMap.insert(_hash_map_pair(2, string("3")));

	system("pause");
	return 0;
}


2015-2-13 15:32:47更新:

	std::map<int, int> tmpMap;
	std::map<int, int>::iterator b;
	tmpMap[1] = 1;
	b = tmpMap.begin();
	printf("map size is %d, first pair key-value is %d-%d.\n", tmpMap.size(), b->first, b->second);
	tmpMap[1] = 2;
	b = tmpMap.begin();
	printf("map size is %d, first pair key-value is %d-%d.\n", tmpMap.size(), b->first, b->second);
以上代码的打印结果为:

map size is 1, first pair key-value is 1-1.
map size is 1, first pair key-value is 1-2.
请按任意键继续. . .

可以发现,在std::map的使用的时候,如果用下标的形式操作一组key-value,如果这组不存在,则进行插入操作,如果存在,则会更新。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值