I have a vector like below
vector<unsigned int> myvector;
vector<unsigned int>::iterator it;
//fill it
for (i=1; i<=10; i++) myvector.push_back(i);
//I want delete first one
it = myvector.begin();
myvector.erase(it++);
//I want to know if I am in last element
if(it != myvector.end())
cout << "a test";
but in compare expression my program crashes. why?
Because
erase()invalidates the iterators at the and after the point of erasure.OTOH, the return value is an iterator that points at the element after the erased one, so you can do it like this: