There are many examples of this online:
void Erase (Vector<int> &v) {for (Vector<int>::reverse_iterator ri=v.rbegin (); Ri!=v.rend ();) {if (*ri% 2 = = 0) {cout << "erasing" << *ri << endl;v.erase ((++ri). Base ()); Convert reverse iterator to positive}else{++ri using the base () function;}}
However, in my own compilation environment is always abnormally interrupted, suggesting that vector iterators incompatible, the reason for this error is that the iterator is incompatible with the container or invalid.
Analyze the erase function to return a pointer to the next element, as long as the pointer is converted to a reverse iterator.
void Erase (Vector<int> &v) {for (Vector<int>::reverse_iterator ri=v.rbegin (); Ri!=v.rend ();) {if (*ri% 2 = = 0) {cout << "erasing" << *ri << Endl;ri = Vector<int>::reverse_iterator (V.erase ((++ri). Base ( ))); Delete to reset iterator}else{++ri;}}}
Additional positive deletions:
for (Vectortype::iterator it = Somevector.begin (); It! = Somevector.end ();) { if (*it== value) { it = Somevec Tor.erase (it); } else { ++it; }}
STL uses iterators to reverse-remove