STL을 사용하여 프로그래밍 진주의 열 15에 프로그램을 다시 만들려고합니다. 문자열 및 인덱스 벡터를 사용하여 접미어 배열을 만들려고합니다. 내가 읽은 단어 목록을 input이라는 문자열에 기록합니다.이 단어는 프로그램 시작 부분에서 stdin에서 읽은 단어 목록으로 작동합니다. 모든 코드가 정렬 될 때까지 예상대로 작동합니다. 나는 STL의 정렬 알고리즘을 사용하고 싶지만, 내가 만드는 것처럼 보이는 seg fault에서 완전히 당혹 스럽다.STL 정렬 알고리즘이 포함 된 SegFault 보류
나는이 :
vector<unsigned int> words;
및 글로벌 변수는
string input;
나는 비교 내 사용자 정의 기능 :
bool wordncompare(unsigned int f, unsigned int s) {
int n = 2;
while (((f < input.size()) && (s < input.size()))
&& (input[f] == input[s])) {
if ((input[f] == ' ') && (--n == 0)) {
return false;
}
f++;
s++;
}
return true;
}
나는 코드를 실행하면 :
sort(words.begin(), words.end());
,691을
프로그램이 부드럽게 종료됩니다. 내가 코드를 실행할 때
그러나 :
sort(words.begin(), words.end(), wordncompare);
을 나는 깊은 STL 내에서 세그먼트 폴트를 생성합니다.
GDB 백 추적 코드는 다음과 같습니다
#0 0x00007ffff7b79893 in std::string::size() const() from /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/libstdc++.so.6
#1 0x0000000000400f3f in wordncompare (f=90, s=0) at text_gen2.cpp:40
#2 0x000000000040188d in std::__unguarded_linear_insert<__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >, unsigned int, bool (*)(unsigned int, unsigned int)> (__last=..., __val=90, __comp=0x400edc <wordncompare(unsigned int, unsigned int)>)
at /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:1735
#3 0x00000000004018df in std::__unguarded_insertion_sort<__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >, bool (*)(unsigned int, unsigned int)> (__first=..., __last=..., __comp=0x400edc <wordncompare(unsigned int, unsigned int)>)
at /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:1812
#4 0x0000000000402562 in std::__final_insertion_sort<__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >, bool (*)(unsigned int, unsigned int)> (__first=..., __last=..., __comp=0x400edc <wordncompare(unsigned int, unsigned int)>)
at /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:1845
#5 0x0000000000402c20 in std::sort<__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int, std::allocator<unsigned int> > >, bool (*)(unsigned int, unsigned int)> (__first=..., __last=..., __comp=0x400edc <wordncompare(unsigned int, unsigned int)>)
at /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:4822
#6 0x00000000004012d2 in main (argc=1, args=0x7fffffffe0b8) at text_gen2.cpp:70
내가 다른 프로그램에서 유사한 코드를 가지고 있지만, 그 프로그램에 내가 벡터 대신 벡터를 사용하고 있습니다. 내 인생에서 내가 뭘 잘못하고 있는지 알 수 없다. 감사!
입력 변수 란 무엇입니까? –
죄송합니다. 방금 더 많은 설명을 제공하기 위해 질문을 업데이트했습니다. –
'k'변수 란 무엇입니까? –