【发布时间】:2016-02-01 05:09:25
【问题描述】:
我为链表哈希表创建了一个bool contains(string) 方法,该方法检查值是否在哈希中。我使用辅助函数进行递归,但是当辅助函数返回 false 时,bool contains(string) 仍然返回 true。我通过调试器运行它,我可以清楚地看到它返回 false,我不知道为什么。
这是正在搜索的当前节点:
"laccoliths"->"morbiferous"->"oculi"->"unscabbarded"
我要搜索的值是"typung"。
代码如下:
bool contains_h(string x, node * p) //helper method
{
if (p == NULL)
return false;
else if (x == p->data)
return true;
else
contains_h(x, p->next);
}
bool contains(string word) { return contains_h(word, head); }
【问题讨论】:
-
启用编译器警告(并阅读它们)可以防止这个错误。
标签: c++ boolean return-value boolean-logic boolean-expression