0

我目前正在用 C++ 实现一个带有二次探测的哈希表。我首先实现了一个相当简单的哈希函数:将我的键(=字符串)的每个字母的 ASCII 值相加。因为我知道这根本不是一个好的哈希函数,所以我现在正在寻找一个更好的哈希函数。我已经用谷歌搜索了一段时间,但我似乎找到的都是类似的简单的。有人可以建议我一个好的哈希函数吗?使用 ASCII 值来计算索引是否有意义?还是应该改用单词的长度?

像我一样在单独的函数中实现碰撞处理是否有意义,或者我应该在 hashtfunction 本身中执行此步骤?

谢谢你的帮助!

int Hash::quadSond(int index, int i)
{
    int newIndex = index + (int)pow(i, 2); 
    return newIndex;
}


int Hash::hashFunction(std::string key) 
{
    int hash = 0;
    int index;
    int k = 1; 

    for (size_t i = 0; i < key.length(); i++) 
    {
        hash += (int)key[i]*5; 
    }
    
    index = hash % m_tableSize;  
    
    if (m_table[index] != nullptr) {
        while (m_table[index] != nullptr) { 
            int newIndex = quadSond(hash, k); 
            index = newIndex % m_tableSize;
            k++;
        }
    }
    
    return index;
}
4

0 回答 0