나는 C++에서 2 개의 파일로부터 입력을 받는다. 그런 다음 두 번째 입력 파일이 위상 정렬인지 식별하십시오. 하지만 어떻게 든 while 루프 문에서 list.empty()를 사용하면 세그먼트 오류가 발생하지만 for 루프는 나에게 오류를주지 않습니다. 그러나 for 루프는 한 번만 반복됩니다. 이후 두 번 통과해야 할 수도 있습니다.while 루프의 분할 오류
#include <list>
#include <iostream>
#include <vector>
using namespace std ;
list<unsigned> output;
list<unsigned> &
testSort (istream & idata , istream & sdata)
{
unsigned n,x1,x2;
vector< list<unsigned> > successor(n);
vector<unsigned> count(n,0);
vector<bool> marks(n,false);
idata >>n;
for(int i=0;i<n;i++) {
idata>>x1>>x2;
count[x2]++;
successor[x1].push_back(x2);
if(idata.eof()) break;
}
for(int i=0;i<n;i++) {
sdata>>x1;
if(count[x1]==0) {
marks[x1]=true;
//for(int j=0;j<successor[x1].size();++j) {
while(!successor[x1].empty()) {
count[successor[x1].front()]--;
successor[x1].pop_front();
}
}
else {
for(int i=0;i<n;i++)
{
if(marks[i]==false)
output.push_back(i);
}
break;
}
}
return output;
}
세그먼트 결함이 발생할 때 가장 먼저해야 할 일은 디버거에서 프로그램을 실행하는 것입니다. 이렇게하면 크래시의 정확한 위치를 정확하게 파악하는 데 도움이 될뿐만 아니라 변수를 검사하여 원인을 파악할 수 있습니다. –