我正在通过模板创建一个 HashTable 类。虽然,在处理字符串或任何数字数据类型(更多 - 所以只是整数)时,我似乎无法找到保持模板性质的方法。只要 HashKey 是字符串类型,这就是我拥有的代码。
template<typename HashKey>
size_t HashTable<HashKey>::myhash(const HashKey & x) const
{
unsigned int hashVal = 0;
for (unsigned int i = 0; i < x; i++)
hashVal = (hashVal << 5) ^ x[i] ^ hashVal;
return hashVal % hashTable.size();
};
有没有办法让它使用类似的东西工作
unsigned int hashVal = 0;
hashVal = (hashVal << 5) ^ x ^ hashVal;
return hashVal % hashTable.size();
这里有什么帮助吗?