New Skill get!
Problem
For containers inside C + +, we can use iterator for easy traversal. But when we make changes through iterator to Vector/map and so on, we need to be careful, because operations often lead to iterator failure, and subsequent behavior becomes unpredictable. Like what:
#include <iostream> #include <vector>using namespace Std;int main () { vector<int> a = {12, 23, 34, 45 ,---------- for (Auto iter = A.begin (); ITER! = A.end (); ++iter) { if (*iter >) { a.erase (ITER); } } for (const auto &element:a) { cout<<element<<endl; } return 0;} Output: 1223456789
The description of std::vector::erase in Cplusplus's reference is:
Iterators, pointers and references pointing to position (or first) and beyond is invalidated, with all Iterators, pointers and references to elements before position (or first) is guaranteed to keep Referri Ng to the same elements they were referring to before the call.
Only the iterator in front of the deleted element remains valid, and subsequent traversal behavior is unpredictable.
Solution Solutions
For vectors, Erase will return to the next iterator, so we can use the following method:
#include <iostream> #include <vector>using namespace Std;int main () { vector<int> a = {12, 23, 34, 45 ,---------- Auto iter = A.begin (); while (iter! = A.end ()) { if (*iter >) { iter = a.erase (ITER); } else { ++iter; } } for (const auto &element:a) { cout<<element<<endl; } return 0;} Output: 1223
For map, deleting iterator only affects the current iterator, so using a for loop is sufficient, such as:
#include <iostream> #include <map>using namespace Std;int main () { Map<int, int> a = {{1, 12}, {2, 23} , {3, (+), {4, +}, {5, 6, +}}; for (Auto iter = A.begin (); ITER! = A.end (); ++iter) { if (Iter->second >) { a.erase (ITER); } } For (const auto &element:a) { cout<<element.first<< ":" <<element.second<<endl; } return 0;} Output: 1:122:23
But it's more recommended to have iterator point to the next element before erase.
#include <iostream> #include <map>using namespace Std;int main () { Map<int, int> a = {{1, 12}, {2, 23} , {3, (+), {4, +}, {5, 6, +}}; Auto iter = A.begin (); while (iter! = A.end ()) { if (Iter->second >) { a.erase (iter++); } else { ++iter; } } for (const auto &element:a) { cout<<element.first<< ":" <<element.second<<endl; } return 0;} Output: 1:122:23
Resources
Http://stackoverflow.com/questions/4645705/vector-erase-iterator
Http://stackoverflow.com/questions/4600567/how-can-i-delete-elements-of-a-stdmap-with-an-iterator