struct StringCompare
{
//define hash function for strings
enum
{
//parameters for hash table
bucket_size = 4, // 一個桶4byte長度(因為sizeof(char*)=4)
min_buckets = 8 // 最少存在8個桶
};
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;
}
}
};
{
//define hash function for strings
enum
{
//parameters for hash table
bucket_size = 4, // 一個桶4byte長度(因為sizeof(char*)=4)
min_buckets = 8 // 最少存在8個桶
};
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>