title: C++ how to erase from vector while iterating
body: <pre><code>std::vector&lt;int *&gt;intList;
  int *a = new int;
  *a = 3;
  int *b = new int;
  *b = 2;
  int *c = new int;
  *c = 1;
  
  intList.push_back(a);
  intList.push_back(b);
  intList.push_back(c);
  
  std::vector&lt;int *&gt;::iterator it;
  
  for (it = intList.begin(); it != intList.end();)
  {
      int *a = (int*)*it;
  
      if ((*a) == 2)
      {
          delete a;
          intList.erase(it);
      }
      else
      {
          ++it;
      }
  }
  </code></pre>
  
  <p>This code crashes at the start of the for loop when something is erased from the vector. I do not understand why. I am using Visual Studio 2015 if that helps</p>