표현식 : 문자열 반복기는 Dereferencible이 아닙니다
-
21-08-2019 - |
문제
C ++의 std :: string :: iterators를 사용하는 데 어려움을 겪고 있습니다. 이 코드는 DEV-C ++에서 정상으로 컴파일 (여전히 올바른 출력을 얻지 못하지만 내 잘못 : TODO, 수정 알고리즘). 런타임 오류가 발생하지 않습니다. 오류는 Visual Studio Express 2008 C ++에 오류가 있는데, 여기서 <xstring> : "expression : string iterator dereferenceable"을 가리키는 오류가 발생하고 <xstring> 파일의 112 행을 가리 킵니다.
내 디버깅은 문장 입력의 끝을 지나서 피해를 입히려고 노력하고 있다고 말하지만 어디에 있는지 알 수 없습니다. 누구든지 약간의 빛을 흘릴 수 있습니까?
std::string wordWrap(std::string sentence, int width)
{
std::string::iterator it = sentence.begin();
//remember how long next word is
int nextWordLength = 0;
int distanceFromWidth = width;
while (it < sentence.end())
{
while (*it != ' ' && it != sentence.end())
{
nextWordLength++;
distanceFromWidth--;
it++;
}
if (nextWordLength > distanceFromWidth)
{
*it = '\n';
distanceFromWidth = width;
nextWordLength = 0;
}
//skip the space
it++;
}
return sentence;
}
해결책
먼저, 오퍼레이터가 아닌 반복자에서 연산자! = ()를 사용합니다. <() :
while (it != sentence.end())
둘째, 이것은 거꾸로됩니다. while (*it != ' ' && it != sentence.end())
반복자가 유효한지 확인하는 것보다 반복자로 무언가를합니다. 대신, 먼저 유효한 지 확인해야합니다.
while (it != sentence.end() && *it != ' ')
셋째, ITERATOR ++를 통해 ++ 반복기를 사용해야하지만 이는 충돌과 관련이 없습니다.
넷째, 주요 문제는 다음과 같습니다.
*it = '\n';
이전 수표로 인해 while (it != sentence.end()
, 끝에있는 동안 반복자 불일치에 도달 할 수 있습니다. 해결책은 다음과 같습니다.
if (it != sentence.end() && nextWordLength > distanceFromWidth)
이제 끝에 도달했다면 멈 춥니 다.
이전 문제를 해결 한 후 이제 유일한 문제는 다음과 같습니다.
//skip the space
++it;
이것은 당신이 건너 뛰는 캐릭터가 실제로 공간이라고 가정합니다. 그러나 문자열의 끝은 어떻습니까? 이 문자열 로이 함수를 실행합니다.
"a test string " // <- space at end
그리고 그것은 성공할 것입니다. 그것은 공간을 건너 뛰고 반복자를 넣습니다 end()
, 루프가 종료되고 성공.
그러나 공간이 없으면 끝에 도달했고 끝을 넘어 건너 뛰기 때문에 충돌합니다. 수정하려면 수표를 추가하십시오.
//skip the space
if (it != sentence.end())
{
++it;
}
이 최종 코드 결과 :
std::string wordWrap(std::string sentence, int width)
{
std::string::iterator it = sentence.begin();
//remember how long next word is
int nextWordLength = 0;
int distanceFromWidth = width;
while (it != sentence.end())
{
while (it != sentence.end() && *it != ' ')
{
nextWordLength++;
distanceFromWidth--;
++it;
}
if (it != sentence.end() && nextWordLength > distanceFromWidth)
{
*it = '\n';
distanceFromWidth = width;
nextWordLength = 0;
}
//skip the space
if (it != sentence.end())
{
++it;
}
}
return sentence;
}
당신은 이것이 많은 중복 점검이있는 것처럼 보일 수 있습니다. 이것은 수정 될 수 있습니다 :
std::string wordWrap(std::string sentence, int width)
{
std::string::iterator it = sentence.begin();
//remember how long next word is
int nextWordLength = 0;
int distanceFromWidth = width;
while (it != sentence.end())
{
while (*it != ' ')
{
nextWordLength++;
distanceFromWidth--;
++it;
// check if done
if (it == sentence.end())
{
return sentence;
}
}
if (nextWordLength > distanceFromWidth)
{
*it = '\n';
distanceFromWidth = width;
nextWordLength = 0;
}
//skip the space
++it;
}
return sentence;
}
잘만되면 그것은 도움이되기를 바랍니다!
다른 팁
while (*it != ' ' && it != sentence.end())
변경
while (it != sentence.end() && *it != ' ')
따라서 두 번째 표현식은 첫 번째 IF False 인 경우 평가되지 않습니다.
if (nextWordLength > distanceFromWidth)
아마도 변경해야합니다
if (it == sentence.end())
break;
if (nextWordLength > distanceFromWidth)
거의 확실히 당신의 오류는 다음의 결과입니다.
*it = '\n';
앞의 동안 루프가 정지 조건 중 하나는 다음과 같습니다.
it != sentence.end()
iT == sentence.end (), 그렇다면 *it = ' n'은 날지 않을 것입니다.
더 많은 오류가 있지만 현재 문제를 일으키는 오류입니다.