1. Introduction of:
The STL map has a erase method to remove the established node from a map
eg
map<string,string> maptest; typedef map<string,string>:: Iterator ITER; ITER iter=maptest.find (key); Maptest.erase (ITER);
Like the above delete a single node, map behavior will not be problematic, but when used in a loop, it is often misused.
2. Traps
eg
for (Iter Iter=maptest.begin (); Iter!=maptest.end (); +iter) { cout<<iter->first<<":"<<iter->second<< Endl; Maptest.erase (ITER); }
This is a wrong way of writing, cause the program behavior is unknown, because map is an associative container, for the associated container, if an element has been deleted, then its corresponding iterator is invalidated, should not be used, responsible for the program will cause undefined behavior.
3. Correct usage
Correct usage One: (c++11 can, c++98 not)
The erase () member function returns an iterator to the next element
STD::MAP<STD::string, std::string >::iterator it = maptest.begin (); while (It! = maptest.end ()) { if(TestVal (it->second)) { = maptest.erase (it); } Else It++ ; }
Correct usage Two:
Locates the next element using the iterator before the delete. How STL recommendations are used
for // Note that you can't write iter++ here anymore . { cout<<iter->first<<":"<<iter->second<< Endl; Maptest.erase (ITER+ +) ; }
In this usage, the method takes advantage of the Post + + feature, which executes Maptest.erase (it++) at this time; This statement is divided into three processes
1, first assign the value of it to a temporary variable as a parameter variable passed to erase
2. Because the parameter processing takes precedence over the function call, then the it++ operation is performed, that is, it now points to the next address.
3. Call the Erase function again to release the location of the temporary variable that was saved in the first step for the value of it to be deleted.
4. Example
CASE1:?
#include <map>#include<iostream>intMain () {Std::map<int,int>map_a; Std::map<int,int>:: Iterator it; for(inti =0; I! =Ten; i++) {Map_a.insert (Std::map<int,int>:: Value_type (I, i)); } for(it = Map_a.begin (); It! = Map_a.end (); it++) {Std::cout<< It->first <<Std::endl; //map_a.erase (it->first); } return 0;}
Results:
0 1 2 3 4 5 6 7 8 9
CASE2:?
#include <map>#include<iostream>intMain () {Std::map<int,int>map_a; Std::map<int,int>:: Iterator it; for(inti =0; I! =Ten; i++) {Map_a.insert (Std::map<int,int>:: Value_type (I, i)); } for(it = Map_a.begin (); It! = Map_a.end (); it++) {Std::cout<< It->first <<Std::endl; Map_a.erase (It-First ); } return 0;}
Results:
0 3
When the map size is added to 1000000, the core
5. Causes of unknown program behavior
The erase method of the 5.1 list and vector returns a iterator that points to the next element of the deleted element, except that the 4 of the Map,set,multimap,multiset return void.
But in the c++11 has been corrected, all returned to a iterator
Cause: The operation of the iterator + + is to manipulate the node pointer it holds, so when the node is deleted, the pointer to the node is not deterministic, that is, it may be the previous data, or it may have other irrelevant data, such behavior is called undefined behavior, not necessarily the core.
C + + std::map::erase usage and its pitfalls