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;
}
编译和运行: