What happens to an STL iterator when the list it is referring to changes, by adding and erasing elements (that are of course not being currently pointed by the iterator)? Is it still valid to use and erase the element pointed by the iterator? What I am really interested in is if the following code is legal:
Code:
#include <list>
#include <iostream>
using namespace std;
int main() {
list<int> fooList;
list<int>::iterator iter;
fooList.push_back(1);
fooList.push_back(2);
iter = fooList.begin();
iter++; // point at the element with value 2
// do some changes to other elements of the list
fooList.pop_front();
fooList.push_back(3);
// the list has changed, but is this still ok?
fooList.erase(iter);
return 0;
}
The code runs fine but I wish to make sure its not just on my platform/implementation.