Hey everybody. I understand that erasing elements in the vector invalidates the iterators. If you consider the following code:
It works for me if I run it, but theoretically speaking, would it be possible for the iterator it to ever become invalidated, and cause an error such as the segmentation fault? The reason I ask is because I believe I have this problem in one of my serious programs. Ideas would be cool.Code:#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec, vec2; vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vector<int>::iterator it; vec2 = vec; it = vec.begin(); for(;;) { vec.erase(vec.begin()); vec = vec2; cout << *it << endl; } return 0; }