stdext::hash_map使用字符串(const char*)做key的話,不是只指定一個(gè)compare函數(shù)難么簡單,要給定一個(gè)結(jié)構(gòu)體,其包括hash函數(shù),compare函數(shù),以及“桶設(shè)定”
struct StringCompare
{
//define hash function for strings
enum
{
//parameters for hash table
bucket_size = 4, // 一個(gè)桶4byte長度(因?yàn)閟izeof(char*)=4)
min_buckets = 8 // 最少存在8個(gè)桶
};
size_t operator()(const char* str) const
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
bool operator()(const char *s1, const char* s2) const
{
if (strcmp(s1, s2) == 0)
{
return false;
}
else
{
return true;
}
}
};
然后
stdext::hash_map<const char*, Your Data Type, StringCompare>