c++中字符串到整型的哈希函数
Hash function in c++ for string to int
我正在寻找c++中的哈希函数将字符串哈希为int。我使用CMapStringToPtr,但它有一个名为"GetNextAssoc"的函数,允许检索键作为字符串,这意味着字符串必须存储,它得到这么多的内存。有没有其他哈希函数占用更少的内存并且不存储字符串?
c++有一个内置的哈希函数,用于所有STL哈希容器。
std::hash
PS:你也可以自己制作,只需通过const引用传递字符串并逐个循环其字符,将它们添加到整数中,然后通过一些值进行mod:)
int hash( const string &key, int tableSize) {
int hashVal = 0;
for(int i = 0; i<key.length(); i++)
hashVal = 37*hashVal+key[i];
hashVal %= tableSize;
if(hashVal<0)
hashVal += tableSize;
return hashVal;
}
相关文章:
- 如何为位集找到/实现一个好的哈希函数
- 对于短字符串来说,这是一个很好的哈希函数吗?
- 我在C++中使用提升哈希函数将 3 个双精度组合成一个面临冲突的哈希
- 无冲突的哈希函数
- Qt5 哈希函数在 Windows 7 和 Windows10 上不会返回相同的哈希
- 解决由于在哈希函数中使用了不完整的类型而导致的编译失败
- 使用 Murmur2 64 位哈希函数生成会导致冲突的输入
- 错误:哈希函数必须可使用键类型的参数进行调用
- unordered_set是否适合存储矢量<int>元素的数据结构?如果是这样,我将如何实现哈希函数?
- 为什么C++哈希函数的返回类型是 std::size_t,而不是独立于平台的类型?
- 哈希函数计算
- 哈希函数中同一键的不同值和良好的哈希值排序
- 将 C# 哈希函数转换为C++
- 如何创建用于生成噪声的哈希函数
- 插入不从哈希函数中检索键
- 简单的 C++11 哈希函数无法编译
- 对于这个英语单词列表来说,什么是好的哈希函数?
- std::bitset 哈希函数算法
- 如何生成伪随机 32 字节字符串以用作加密哈希函数中的盐?
- C unordered_map自定义的哈希函数碰撞