给定一个由 ASCII 码和相应的数字值组成的全局向量list
以及一个字符串,例如000.00-000.0.0.0
,这个函数接受一个input
2-char 或 3-char 长的标记字符串,并将其替换为一个表示 0 到 184 之间的数字值的 ASCII 符号,然后将不带分隔符的缩短字符串返回为out
. 此外,在给定 ASCII 符号的反向(方向 1)中,它会转换回数字字符串并返回。
//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 char 添加到 out 字符串时出现错误,因为 temp 是 == 1char,所以它应该按原样返回vectorSearch()
。然后清除 和 的返回convertOntology()
检查。但是,它永远不会在第一行中断,并且有一个input
temp == ""
vectorSearch()
Unhandled exception at 0x77bc15de exception: std::out_of_range at memory location 0x0035cf1c
到底是怎么回事?这是通过返回进行递归回溯的问题吗?我在某处缺少返回以打破递归循环?