給定一個全局矢量的ASCII代碼和相應的數字值和一個字符串list
,像000.00-000.0.0.0
,該功能需要一個input
令牌字符串2炭或3個字元長和具有單ASCII符號替換它表示0到184之間的數字值,然後返回不帶刪除符的縮短字符串,如out
。另外,在反向(方向1)給定ASCII符號時,它將轉換回數字字符串並返回。遞歸函數具有out_of_range例外
//looks for input string in vector and returns output, 'c' is check row, 'r' is return row
string vectorSearch(string &check, int n, int c, int r)
{
if (check.length() <= 1)
return check;
if (list[n][c] == check || list[n][c] == ('0'+check)) //adds leading zero if 2char long
return list[n][r];
else
return vectorSearch (check, ++n, c, r);
}
//this function takes an ontology and either changes from single char
//to string or takes strings and converts to char representation
string Lexicon::convertOntology(string input, int direction, string out, string temp)
{
if (input == "" && temp == "")
return out; //check for completed conversion
else {
if (input[0] == '.' || input[0] == '-' || input == "") { //found deliniator or endk
if (input != "") return convertOntology(input.substr(1),direction,
out+=vectorSearch(temp, 0, direction, 1-direction), "");
else return convertOntology("", direction,
out+=vectorSearch(temp, 0, direction, 1-direction), "");
} else
return convertOntology(input.substr(1), direction, out, temp+=input[0]); //increment and check
}
}
這些函數在解析最後一個字符後的輸出上工作正常。在線return convertOntology(input.substr(1), direction, out+=add, temp);
中斷時,input == ""
和temp == "0"
- 最後通過vectorSearch()
應該清除temp並將temp字符添加到out字符串中,因爲temp爲== 1char,因此應該從vectorSearch()
返回爲它是。然後清除input
和temp == ""
的convertOntology()
退貨支票。但是,它從來沒有達到上vectorSearch()
第一線休息,有一個
Unhandled exception at 0x77bc15de exception: std::out_of_range at memory location 0x0035cf1c
這是怎麼回事?這是通過返回遞歸回溯的問題,我錯過了某處返回以打破遞歸循環?
我可能已經找到它了;在最後一輪input.substr(1)在字符串上調用「」 – 2012-07-26 15:16:29