我有一个树结构,我想找到符合给定条件的所有节点。每次调用find函数时,它都会返回下一个匹配节点。通过递归函数调用搜索子项。
由于某种原因,对此实现的指针的密钥比较失败。请参阅下面的代码,我已经指出了失败的比较。
HtmlTag* HtmlContent::FindTag(string tagName, string tagParameterContent)
{
if (tagName.empty() && tagParameterContent.empty())
return NULL;
if (this->startTag == NULL)
return NULL;
this->findContinue = this->FindChildren(this->startTag, &tagName, &tagParameterContent);
return this->findContinue;
}
HtmlTag* HtmlContent::FindChildren(HtmlTag* firstTag, string* tagName, string* tagParameterContent)
{
HtmlTag* currentTag = firstTag;
HtmlTag* childrenFound = NULL;
while (currentTag != NULL)
{
if (!tagName->empty() && *tagName == currentTag->tagName)
{
if (tagParameterContent->empty() || currentTag->tagParameters.find(*tagParameterContent, 0) != -1)
{
if (this->findContinue == NULL)
break; // break now when found
else if (this->findContinue == currentTag) // TODO why this fails?
this->findContinue == NULL; // break on next find
}
}
if (currentTag->pFirstChild != NULL)
{
childrenFound = this->FindChildren(currentTag->pFirstChild, tagName, tagParameterContent);
if (childrenFound != NULL)
{
currentTag = childrenFound;
break;
}
}
currentTag = currentTag->pNextSibling;
}
return currentTag;
}
VC ++编译器接受此代码,但出于某种原因,我不能在此比较中设置断点。我想这是优化的,但为什么呢?为什么这种比较会失败?
答案 0 :(得分:4)
我认为你应该在比较之后用赋值替换==。编译器优化了整个部分,因为它没有做任何有用的事情。