該函數採用包含'*'和'?'的字符串wild
通配符,並用nodeT *w
替換來自樹型數據庫的可能字符的通配符。 out
保存一個臨時字符串。每個候選人都被添加到引用的bst。遞歸函數堆棧溢出
void Lexicon::matchRegExpHelper(nodeT *w, string wild, Set<string> &matchSet, string out)
{
if (wild == "") matchSet.add(out);
else {
if (wild[0] != '*' || wild[0] != '?') { //this parses up to the wildcard, earlier versions used a position parameter and looped through the prefix chars recursively
for (int j = 0; j < w->alpha.size(); j++)
if (wild[0] == w->alpha[j].letter) matchRegExpHelper(w->alpha[j].next, wild.substr(1), matchSet, out+=wild[0]);
} else {
for (int i = 0; i < w->alpha.size(); i++) {
if (wild[0] == '?') matchRegExpHelper(w->alpha[i].next, wild.substr(1), matchSet, out+=w->alpha[i].letter);//follow path
else { //logically, wild[0] == '*' must be true
if (ontLength == (wild.length() + out.length())) matchRegExpHelper(w->alpha[i].next, wild.substr(1), matchSet, out+=w->alpha[i].letter); //ontology is full, treat like a '?'
else matchRegExpHelper(w->alpha[i].next, wild.substr(1), matchSet, out+=(w->alpha[i].letter+'*')); //keep adding chars
}
}
}
}
}
當達到第一個通配符功能重新開始 - 我曾試圖與for循環改寫這一點,沒有循環,並在不同的「修剪」的做法。我缺少一些基本的東西,並懷疑這是一個回溯問題。最終堆棧溢出。
問題:1)我在概念上缺少什麼,2)我該如何解決這個功能?
版本沒有for循環 - 測試用例是有點不同,但相似的,我不得不測試它找了一遍
else {
if (wild[0] == '?'){
matchRegExpHelper(w, wild, ++pos, matchSet, out);//return and check next path
matchRegExpHelper(w->alpha[pos].next, wild.substr(1), 0, matchSet, out+=w->alpha[pos].letter);//follow path
}
if (wild[0] == '*'){
matchRegExpHelper(w, wild, ++pos, matchSet, out);//return and check next path
if (ontLength == (wild.length() + out.length()))matchRegExpHelper(w->alpha[pos].next, wild.substr(1), 0, matchSet, out+=w->alpha[pos].letter); //ontology is full, treat like a '?'
else matchRegExpHelper(w->alpha[pos].next, wild.substr(1), 0, matchSet, out+=(w->alpha[pos].letter+'*')); //keep adding chars
}
if (wild[0] == w->alpha[pos].letter) matchRegExpHelper(w->alpha[pos].next, wild.substr(1), 0, matchSet, out+=wild[0]);
matchRegExpHelper(w, wild, ++pos, matchSet, out);//check next path
}
for (int i = 0; i < w->alpha.size(); i++) matchRegExpHelper(w->alpha[i].next, wild.substr(1), 0, matchSet, out+=wild[0]);//step over char
的在端部線圈是固定的溢出企圖,我想也許沒有一些線程的情況下,但我希望那些修剪,所以不知道該怎麼辦
您是否嘗試過使用調試器? – 2012-07-27 10:44:59
@Basile_Starynkevitch也許我不知道dubugger是什麼,但我按下了'開始調試'的綠色箭頭 - 所以我認爲答案是肯定的。 – 2012-07-27 10:53:17
您需要了解如何使用開發工具進行調試。花時間閱讀一些文檔不會有害。您需要放置斷點並查看回溯。 – 2012-07-27 10:54:52