2012年(158)
分类: C/C++
2012-11-23 15:07:06
#define TESTHASHMAP 1 // 为0使用map,为1使用hash_map,可以对照看看结果
#if TESTHASHMAP != 0
#include
using namespace
__gnu_cxx;
#else
#include
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
#else
typedef map
#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