1

ASCII 코드와 해당 숫자 값 및 문자열 (예 : 000.00-000.0.0.0)의 글로벌 벡터 list이 제공된 경우이 함수는 input 토큰 문자열 2-char 또는 3-char long을 취해 하나의 ASCII 심볼로 바꿉니다 0에서 184 사이의 숫자 값을 나타내는 다음 delimator없이 단축 문자열을 out으로 반환합니다. 또한 ASCII 심볼이 주어진 역순으로 (방향 1) 숫자 문자열로 다시 변환하여 반환됩니다.재귀 함수의 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 
    } 
} 

이러한 함수는 마지막 char가 구문 분석 된 후 출력시를 제외하고는 정상적으로 작동합니다. return convertOntology(input.substr(1), direction, out+=add, temp);의 중단으로 인해 및 temp == "0" 일 때 오류가 발생합니다. 마지막 통과시 vectorSearch()은 임시 문자를 지우고 임시 문자열을 out 문자열에 추가해야합니다. 임시 문자는 == 1char이므로 vectorSearch()에서 반환해야합니다. 그것은. 그런 다음 convertOntology() 반품 확인을 inputtemp == ""으로 삭제하십시오. 그러나, 그것은 무슨 일 vectorSearch()의 첫 번째 줄에 휴식을 도달하지와

Unhandled exception at 0x77bc15de exception: std::out_of_range at memory location 0x0035cf1c 

이 결코? 이것은 반환을 통한 재귀 재귀와 관련된 문제이며 재귀 루프를 어딘가에서 되돌려 놓고 있습니다.

+0

내가 발견했을 수도 있습니다. 마지막 라운드에서 input.substr (1)은 문자열 ""에 호출됩니다. –

답변

2

temp == ""input != ""의 경우 input.substr(1)이라고 부르는 경우가 있습니다. input 문자열이 한 문자 긴 정확하게 경우

1

당신이 다른 부분에 도착하지 않더라도 ,

input.substr(1) 

예외가 발생합니다.

- input.substr(input.size())이 허용되지 않으며 비어있는 문자열을 반환합니다.


나중에 VectorSearch에서 비슷한 문제가 발생할 수 있습니다. 일치하는 항목이 없으면 범위를 벗어날 때까지 n을 증가시킵니다.

+0

일치하지 않는 항목을 catch합니다. 즉 다음 버전에 저장되는 기능입니다. 나는 1 숯으로 input.substr (1)을 테스트 할 것이고, 이것은 첫 번째 라운드에서 괜찮은 것 같았다. –

+0

이 수정본을 추가했습니다. if (check.length() <= 1 || n > 200) –

+0

예, 잘못되었습니다. 실제로는 괜찮습니다. substr (pos)의'pos '에 대한 요구 사항은'pos <= size()'입니다.'pos

관련 문제