Solution

The problem lies in the fact that erasing invalidates all subsequent iterators. In order to erase some iterators subject to certain conditions, one must first remove them to the end of the vector and then erase the end.


#include<vector>
#include<iostream>
#include<algorithm>
int main(int argc, char** argv) {
  using namespace std;
  int ret = 0;
  vector<int> theVector;
  theVector.push_back(2);
  theVector.push_back(1);
  vector<int>::iterator theEnd = remove(theVector.begin(), theVector.end(), 1);
  theVector.erase(theEnd, theVector.end());
  for(int i = 0; i < theVector.size(); i++) {
    cout << theVector[i] << endl;
  }
  return ret;
}



Leo Liberti 2008-01-12