In practice, the vector series in c ++ -- the second worst iterator failed (deleting elements in the vector)
Directly refer to the Code:
# Include
# Include
Using namespace std; int main () {vector
V; v. push_back (1); v. push_back (2); v. push_back (3); std: vector
: Iterator iter1 = v. begin () + 2; v. erase (v. begin () + 1); int n = * iter1; return 0 ;}
After erase, The iterator becomes invalid.
Several Failures of the vector iterator:
1. When an element is inserted (push_back), The iterator returned by the end operation is definitely invalid.
2. When an element is inserted (push_back), the return value of capacity is different from that before the element is inserted. Then, the entire container needs to be reloaded, And the iterator returned by the first and end operations will become invalid.
3. After the delete operation (erase, pop_back) is performed, all the iterators pointing to the delete vertex are invalid. All the iterators pointing to the elements behind the delete vertex are also invalid.