1. map.end () the address after the last element of the map, whether executed map.erase (ITER) or Map.add (key, value),map.end () The returned value never changes, pointing to the same block of memory.
2. Map.begin () points to the first element of map,Map.begin () may change with map.erase (ITER) or Map.add (key, value) operation. For example, when the first element is deleted,map.begin () changes, pointing to the element that followed the original first element. Or if a new key-value pair is inserted, the key of the pair is placed in front of the Map.begin ()->first in the btree (we assume that the map is implemented internally by Btree, and there may actually be other implementations). Then Map.begin () will also point to the newly inserted key-value pair .
3. After the execution of Map.erase (ITER) , the current ITER loses its meaning, and the execution of ++iter will be problematic.
The experiment code is as follows:
#include <map>#include<string>#include<stdio.h>#include<iostream>using namespacestd;#defineNUM 6stringString_trim (Const string&data,Const Char*space) {size_t Begin_pos=data.find_first_not_of (space); size_t End_pos=data.find_last_not_of (space); if(Begin_pos! =string:: NPOs) { returnData.substr (begin_pos,end_pos-begin_pos+1); } return string("");}intMain () {map<int,int>MyMap; for(intI=1; i<num; ++i) {Mymap.insert (pair<int,int> (i* +, i* -)); }/*for (int i=0; i<num; ++i) {Mymap.insert (Pair<int, int> (i,i*10000)); }*/ intn =8; Map<int,int>::iterator iter =Mymap.begin (); Map<int,int>::iterator iterend = Mymap.end ();//save Iterend First to compare whether Mymap.end () will changemap<int,int>::iterator Iterbegin =ITER; //printf ("minux=%d\n", iterend-iter);Mymap.insert (pair<int,int> (100000,1)); if(iterend==Mymap.end ()) {printf ("AAA not changed end () \ n"); } for(; Iter!=mymap.end (); + +ITER) { BOOLBeginequal = iter==Iterbegin; BOOLEndequal = (iter==iterend); printf ("key:%d, value:%d\n", Iter->first, iter->second);//printf ("bgeinequal =%d, endequal =%d\n", beginequal, endequal); if(n< -) Mymap.insert (pair<int,int> (n,n*Ten)); ++N; //Mymap.erase (ITER); //iterend = Mymap.end (); //printf ("key:%d, value:%d\n", Iter->first, Iter->second); } if(iter==iterend) {printf ("End Not change\n"); } Else{printf ("End is changed\n"); } if(Iterbegin = =Mymap.begin ()) {printf ("Begin not change\n"); } Else{printf ("Begin is changed\n"); } printf ("---Traverse the contents of the print Mymap-----------\ n"); ITER=Mymap.begin (); for(; Iter!=mymap.end (); + +ITER) {printf ("key:%d, value:%d\n", Iter->first, iter->second); } printf ("finished\n"); return 0;}
C + + map after inserting data, begin (), end () and current iterator changes