c++ - 似乎无法在碰撞、散列时打破 while 循环

标签 c++ loops hash g++

我不知道为什么这段代码没有跳出 while 循环:

int table_size = 953;
store hash_table[953];
for(int i = 0; i < table_size; i++)
    hash_table[i].count = 0;

//bunch of stuff to get hash value here

while(hash_table[hashNum].data != pString || hash_table[hashNum].count != 0){
    hashNum++;
    if(hashNum > table_size)
        hashNum = 0;
    cout << hash_table[hashNum].count; 
    // to check the value of the count in the array, it IS 0, thus should have broken the loop
}

最佳答案

你的意思可能是:

while(hash_table[hashNum].data != pString && hash_table[hashNum].count != 0)

在您的代码中,如果任一情况为真,循环将继续,hash_table[hashNum].count == 0 不足以使子句为假。

关于c++ - 似乎无法在碰撞、散列时打破 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5959296/

相关文章:

java - 在解析期间访问 JSON 数据中的键时,For 循环未完成

Android 调试 key / keystore 已更改,无法找到新 key 的来源

algorithm - 为什么哈希表查找只需要 O(1) 时间,而搜索键是 O(n)?

c# - 使用哪一个 : Managed vs. 非托管哈希算法

Java json对象替换键名称

c++ - 如何从文本文件的末尾删除新行?

c++ - 伪随机数生成器的相同调用之间的不同行为

c++ - 成绩不会显示

c++ - Clang:覆盖之前在命令行中指定的所有警告和警告为错误的标志

javascript - 如何连续显示元素,隐藏它们,然后在 jQuery 中重复