【STL源码剖析】--CentOS系统hashmap测试用例

CentOS6.4系统,GCC版本:


测试代码如下:

//#include <tr1/unordered_map>	//#include <hash_map>替换为#include <tr1/unordered_map>
//using namespace std::tr1;
#include <ext/hash_map>	//编译时候提示This file includes at least one deprecated or antiquated
						//header which may be removed without further notice at a future date.
#include <cstring>
#include <iostream>
using namespace std;
using namespace __gnu_cxx;	//必不可少

struct eqstr
{
	bool operator()(const char* s1, const char* s2) const {
		return strcmp(s1, s2) == 0;
	}
};

int main(int argc, char **argv)
{
	hash_map<const char *, int, hash<const char*>, eqstr> months;
	
	months["January"] = 31;
	months["February"] = 28;
	months["March"] = 31;
	months["April"] = 30;
	months["May"] = 31;
	months["June"] = 30;
	months["July"] = 31;
	months["August"] = 31;
	months["September"] = 30;
	months["October"] = 31;
	months["November"] = 30;
//	months["December"] = 31;
	months.insert(hash_map<const char *, int>::value_type("December" , 31));
	
	cout << "----------------------------------" << endl;
	cout << "January -> " << months["January"] << endl;
	cout << "September -> " << months["September"] << endl;
	cout << "December -> " << months["December"] << endl;
	cout << "----------------------------------" << endl;
	
	hash_map<const char *, int, hash<const char*>, eqstr>::iterator iter1 = months.begin();
	hash_map<const char *, int, hash<const char*>, eqstr>::iterator iter2 = months.end();
	for( ; iter1 != iter2; ++iter1) {
		cout << iter1->first << " --> " << iter1->second << endl;
	}
	//cout << endl;
	return 0;
	
}

编译和运行:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值