【发布时间】:2016-12-01 09:06:57
【问题描述】:
我正在编写线性和二次探测哈希表程序。
这是我用于线性探测功能的 for 循环,它工作得非常好。
//when there's a collision increase i by 1 until finding empty slot
for(i = (hashVal % tableSize+1) % tableSize; i <tableSize; i++)
if(a[i] == -1){
a[i] = hashVal;
break;
}
所以我又在二次探测函数中写了一个for循环来处理碰撞
//when there's a collision increase i by i^2
j = 0;
for(i=((hashVal % tableSize+1) % tableSize); i < tableSize; i++)
j = i^2;
if(a[j] == -1){
a[j] = hashVal;
break;
}
但是当我编译二次探测时,我得到了这个错误
error: 'break' statement not in loop or switch statement
我真的很困惑为什么它会在第二个中导致错误,而在线性探测中却很好。谁能解释为什么?
【问题讨论】:
-
你在 for 循环之后缺少花括号。
标签: c++ if-statement for-loop break