we all know when use erase in for , we must reset iter, like iter = vector.erase(iter), since erase option will invalidate iteraor. however i found that, not reset also works, the code belows:
int main() {
vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(2);
a.push_back(10);
a.push_back(11);
for (vector<int>::iterator iter = a.begin(); iter != a.end();) {
if (*iter == 2) {
// iter = a.erase(iter); the same
a.erase(iter);
continue;
} else {
iter++;
}
}
for (vector<int>::iterator iter = a.begin(); iter != a.end(); iter++) {
cout << *iter << " ";
}
cout << endl;
return 0;
}
the code run successfully, with the output: 1 3 10 11.
so my question is while "a.erase(iter)" got the same result with "iter = a.earse(iter)" in this code?