为什么我在这个迭代器中出现分段错误

Why am I getting a segmentation fault in this iterator?

本文关键字:分段 错误 迭代器 为什么      更新时间:2023-10-16

我正在编写一个程序,该程序应该查看一个图,并计算出需要删除的最小边数,以留下一个林,其中每个连接的组都有偶数个顶点。我知道如何解决这个问题,但当我尝试迭代列表时,我会遇到分段错误,无法找出原因。

#include <cmath>
#include <cstdio>
#include <list>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int N;
    cin >> N;
    int M; 
    cin >> M;
    // matrix of adjacency list to hold node values
    vector<list<int> > adjList(N, list<int>());
    // find the number of children nodes each node has
    int ui, vi;
    for (int i = 0; i < M; i++) {
      cin >> ui;
      cin >> vi;
      ui--;
      vi--;
      //cout << ui << " " << vi << endl;
      adjList[ui].push_back(vi);
      adjList[vi].push_back(ui);
      //cout << "list length: " << adjList[ui].size() << endl;
    }
    //cout << "after for loop" << endl;
    // count the number of nodes with even numbers of children
    for (int i = 0; i <= M; i++) {
      cout << i << "-> ";
      for (list<int>::iterator it = adjList[i].begin(); it != adjList[i].end(); it++) {
        cout << *it << " ";
      }
      cout << endl;
    }
    int edgesRemoved = 0;
    for (int i = 0; i <= M; i++) {
      for (list<int>::iterator it = adjList[i].begin(); it != adjList[i].end(); ++it) {
        int j = *it;
        if (adjList[j].size() % 2 == 1) {
          // delete vertex from current list
          cout << "test" << endl;
          adjList[i].erase(it);
          // delete vertex on the other list
          cout << "test" << endl;
          cout << j << endl;
          cout << *adjList[j].begin() << endl;
          for (list<int>::iterator it2 = adjList[j].begin(); it2 != adjList[j].end(); ++it2) {
            cout << *it2 << " ";
            if (i == *it2) {
              adjList[j].erase(it2);
            }
          }
          edgesRemoved++;
        }
      }
    }
    cout << edgesRemoved << endl;
    return 0;
}

在使用cout语句调试程序后,我发现问题就在这里:

for (int i = 0; i <= M; i++) {
      for (list<int>::iterator it = adjList[i].begin(); it != adjList[i].end(); ++it) {
        int j = *it;
        if (adjList[j].size() % 2 == 1) {
          // delete vertex from current list
          cout << "test" << endl;
          adjList[i].erase(it);
          // RIGHT UNDER HERE
          //    vvvvvvvvvv
          for (list<int>::iterator it2 = adjList[j].begin(); it2 != adjList[j].end(); ++it2) {
            cout << *it2 << " ";
            if (i == *it2) {
              adjList[j].erase(it2);
            }
          }
          edgesRemoved++;
        }
      }
    }

程序创建了一个迭代器,用来遍历向量中的另一个列表,之后我遇到了分段错误。我不明白为什么,语法与第一个for循环相同,另一个迭代器遍历第一个列表。

下面是一个例子,说明在我输入表示树的数字后会发生什么,然后程序打印邻接矩阵并继续进行实际计算(这部分工作得很好,这是计算过程中的最终结果):

10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8
0-> 1 2 5 
1-> 0 4 6 
2-> 0 3 
3-> 2 
4-> 1 
5-> 0 7 
6-> 1 
7-> 5 8 9 
8-> 7 
9-> 7 
test
test
1
0
Segmentation fault

删除迭代器下的项将使迭代器无效;使用它会进一步导致未定义的行为,因此任何事情都可能发生。这类事情的常用成语是:

std::list<int>::iterator it = adjList[i].begin();
while ( it != adjList[i].end() ) {
    if ( *it == i ) {
        it = adjList[j].erase( it );
    } else {
        ++ it;
    }
}

erase函数将迭代器返回到被移除的元素之后的元素。

这对所有序列类型都有效,而不仅仅是std::list

在对列表进行迭代时,不能删除列表中的当前项(迭代器指向的对象)

adjList[j].erase(it2++);

然而,afaik,在迭代时既不收缩也不扩展列表被认为是最佳实践。