#define TESTHASHMAP 1 // 为0使用map,为1使用hash_map,可以对照看看结果
#if TESTHASHMAP != 0
#include <ext/hash_map>
using namespace __gnu_cxx;
#else
#include <map>
#endif
#include <iostream>
using namespace std;
struct eqstr
{
#if TESTHASHMAP != 0
bool operator()( const int& s1, const int& s2 ) const // 没有这个函数编译报错,有了嘛却又从来不调用
{
printf( “-” );
return ((s1/10)==(s2/10));
}
#else
bool operator()( const int& s1, const int& s2 ) const
{
return ((s1/10)<(s2/10));
}
#endif
};
int main()
{
#if TESTHASHMAP != 0
typedef hash_map<int, int, hash<int>, eqstr> TYPE;
#else
typedef map<int, int, eqstr> TYPE;
#endif
TYPE test;
test[31] = 31;
test[32] = 32;
test[41] = 41;
for( TYPE::const_iterator itor=test.begin(); itor!=test.end(); ++itor )
{
cout << ‘(‘ << itor->first << ‘,’ << itor->second << ‘)’ << endl;
}
if( test.find(33) != test.end() )
cout << “succeed” << endl;
else
cout << “fail” << endl;
system( “pause” );
}
——————————————————————
使用map输出:
(31,32)
(41,41)
succeed
使用hash_map输出:
(31,31)
(32,32)
(41,41)
fail
>> 本文固定链接: http://www.vcgood.com/archives/1467