title: Why am I getting a segmentation fault in this iterator?
body: <p>I'm writing a program that should look through a graph and calculate the minimum number of edges that need to be deleted to leave a forest where each connected group has an even number of vertices. I know how to solve the problem, but when I try and iterate through a list I get a segmentation fault and can't figure out why.</p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;cstdio&gt;
  #include &lt;list&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  int main() {
      /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  
      int N;
      cin &gt;&gt; N;
      int M; 
      cin &gt;&gt; M;
  
      // matrix of adjacency list to hold node values
      vector&lt;list&lt;int&gt; &gt; adjList(N, list&lt;int&gt;());
  
      // find the number of children nodes each node has
      int ui, vi;
      for (int i = 0; i &lt; M; i++) {
        cin &gt;&gt; ui;
        cin &gt;&gt; vi;
        ui--;
        vi--;
        //cout &lt;&lt; ui &lt;&lt; " " &lt;&lt; vi &lt;&lt; endl;
        adjList[ui].push_back(vi);
        adjList[vi].push_back(ui);
        //cout &lt;&lt; "list length: " &lt;&lt; adjList[ui].size() &lt;&lt; endl;
      }
      //cout &lt;&lt; "after for loop" &lt;&lt; endl;
      // count the number of nodes with even numbers of children
  
      for (int i = 0; i &lt;= M; i++) {
        cout &lt;&lt; i &lt;&lt; "-&gt; ";
        for (list&lt;int&gt;::iterator it = adjList[i].begin(); it != adjList[i].end(); it++) {
          cout &lt;&lt; *it &lt;&lt; " ";
        }
        cout &lt;&lt; endl;
      }
  
      int edgesRemoved = 0;
  
      for (int i = 0; i &lt;= M; i++) {
        for (list&lt;int&gt;::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 &lt;&lt; "test" &lt;&lt; endl;
            adjList[i].erase(it);
  
            // delete vertex on the other list
            cout &lt;&lt; "test" &lt;&lt; endl;
            cout &lt;&lt; j &lt;&lt; endl;
            cout &lt;&lt; *adjList[j].begin() &lt;&lt; endl;
            for (list&lt;int&gt;::iterator it2 = adjList[j].begin(); it2 != adjList[j].end(); ++it2) {
              cout &lt;&lt; *it2 &lt;&lt; " ";
              if (i == *it2) {
                adjList[j].erase(it2);
              }
            }
  
            edgesRemoved++;
          }
        }
      }
      cout &lt;&lt; edgesRemoved &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>After using cout statements to debug the program I figured out that the problem is here:</p>
  
  <pre><code>for (int i = 0; i &lt;= M; i++) {
        for (list&lt;int&gt;::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 &lt;&lt; "test" &lt;&lt; endl;
            adjList[i].erase(it);
  
            // RIGHT UNDER HERE
            //    vvvvvvvvvv
  
            for (list&lt;int&gt;::iterator it2 = adjList[j].begin(); it2 != adjList[j].end(); ++it2) {
              cout &lt;&lt; *it2 &lt;&lt; " ";
              if (i == *it2) {
                adjList[j].erase(it2);
              }
            }
  
            edgesRemoved++;
          }
        }
      }
  </code></pre>
  
  <p>I get a segmentation fault after the program creates an iterator that is meant to go through another list in the vector. I don't understand why though, the syntax is the same as the first for loop with another iterator going through the first list.</p>
  
  <p>Here is an example of what happens after I type in the input of the digits that represent a tree, the program then prints the adjacency matrix and goes on to the actual calculation (this part works fine, it's the end result during calculation):</p>
  
  <pre><code>10 9
  2 1
  3 1
  4 3
  5 2
  6 1
  7 2
  8 6
  9 8
  10 8
  0-&gt; 1 2 5 
  1-&gt; 0 4 6 
  2-&gt; 0 3 
  3-&gt; 2 
  4-&gt; 1 
  5-&gt; 0 7 
  6-&gt; 1 
  7-&gt; 5 8 9 
  8-&gt; 7 
  9-&gt; 7 
  test
  test
  1
  0
  Segmentation fault
  </code></pre>