When a container changes, the iterator pointing to the elements in the container may become invalid. This makes it easy to change the container when the iterator changes. In this regard, different containers provide different protection:
Vectors: causes memory re-allocation of insert operations to invalidate all iterators. insertion also invalidates the iterator at and after the insertion position, the delete operation invalidates the iterator at and after the delete position.
The push_back operation of vector may be okay, but all iterators will become invalid once memory reallocation is triggered;
All iterators after insert points of the insert operation of the vector become invalid. However, once memory redistribution is triggered, all iterators become invalid;
All iterators after the inserted point of the erase operation of vector become invalid;
The Reserve Operation of vector fails for all iterators (because it causes memory redistribution );
LIST/map: insert will not invalidate any iterator; delete operation will invalidate the iterator pointing to the delete location, but will not invalidate other iterators.
All iterators of the insert operation of deque are invalid;
All deque erase operations fail;
1. For associated containers (such as map, set, multimap, and Multiset), deleting the current iterator only invalidates the current iterator. You only need to increment the current iterator at erase. This is because containers such as map are implemented using a red/black tree. inserting or deleting a node will not affect other nodes. The erase iterator is invalid only for the iterator whose elements are deleted, but the returned value is void. Therefore, erase (ITER ++) is used to delete the iterator.
For (iter = Cont. Begin (); it! = Cont. End ();)
{
(* ITER)-> dosomething ();
If (shoulddelete (* ITER ))
Cont. Erase (ITER ++ );
Else
++ ITER;
}
2. For sequential containers (such as vector and deque), deleting the current iterator will invalidate the iterator of all subsequent elements. This is because vetor and deque use the continuously allocated memory. Deleting an element causes all the following elements to move one position forward. Therefore, the erase (ITER ++) method cannot be used. Fortunately, the erase method can return the next valid iterator.
For (iter = Cont. Begin (); iter! = Cont. End ();)
{
(* It)-> dosomething ();
If (shoulddelete (* ITER ))
Iter = Cont. Erase (ITER );
Else
++ ITER;
}
3. For list, it uses non-contiguous memory and Its erase method will return the next valid iterator. Therefore, both methods can be used.
Deletes an element in an array that repeats consecutively, such as, ---> 1, and 0. A correct implementation of the problem is given:
#include <iostream>#include <vector>using namespace std;int main(int argc, char* argv[]){ int a[] = {1, 1, 3, 3, 3, 2, 4, 1, 1, 1, 0}; int size = sizeof(a)/sizeof(a[0]); vector<int> vec(a, a+size); vector<int>::iterator iter = vec.begin(); int previous = *iter; ++iter; for (; iter != vec.end();) { if(*iter == previous) { iter = vec.erase(iter); } else { previous = *iter; ++iter; } } for(iter = vec.begin(); iter != vec.end(); ++iter) { cout << *iter << endl; } return 0;}
PS. But in fact, it is not very suitable to use vector to implement this problem, because each time an element is deleted, it will cause a resize operation on the vector. The time complexity of Resize is O (n), and the entire resize operation takes O (N ^ 2 ). It is best to select list as the most container, and list is the most suitable for examples that need to be inserted or deleted in the container.