Map is C + +a standard container, she provides a very good one-to-ones relationship, in some programs to build a map can play a multiplier effect, summed up some map basic simple and practical operation! 1. Map's most basic constructor; Map<string,int>mapstring; map<int,string>Mapint; Map<sring,Char>mapstring; map<Char,string>Mapchar; Map<Char,int>mapchar; map<int,Char>Mapint;2. Map add data; Map<int,string>maplive; 1. Maplive.insert (pair<int,string> (102,"aclive")); 2. Maplive.insert (map<int,string>::value_type (321,"Hai")); 3, maplive[ the]="April";//The simplest and most commonly used inserts in the map are added! 3, the lookup of elements in map: the Find () function returns an iterator that points to an element with a key value, and returns an iterator to the tail of the map if it is not found. Map<int,string>:: iterator l_it;; L_it=maplive.find ( the); if(l_it==maplive.end ()) cout<<"We do not find"<<Endl; Elsecout<<"wo Find"<<Endl;4, the deletion of elements in map: If you delete the map<int,string>:: iterator l_it;; L_it=maplive.find ( the); if(l_it==maplive.end ()) cout<<"We do not find"<<Endl; ElseMaplive.erase (L_IT);//Delete;5, the use of swap in map: Swap in map is not an element exchange in a container, but a two container exchange; For example: #include<map>#include<iostream>using namespacestd; intMain () {map<int,int>M1, M2, M3; Map<int,int>:: Iterator m1_iter; M1.insert (Pair<int,int> (1,Ten ) ); M1.insert (Pair<int,int> (2, - ) ); M1.insert (Pair<int,int> (3, - ) ); M2.insert (Pair<int,int> (Ten, - ) ); M2.insert (Pair<int,int> ( -, $ ) ); M3.insert (Pair<int,int> ( -, - ) ); cout<<"The original map M1 is:"; for(M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++) cout<<" "<< m1_iter->second; cout<<"."<<Endl; //This is the member function version of Swap//M2 is said to be the argument map; M1 the target mapM1.swap (m2); cout<<"After swapping with M2, map M1 is:"; for(M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++) cout<<" "<< M1_itersecond; cout<<"."<<Endl; cout<<"After swapping with M2, map m2 is:"; for(M1_iter = M2.begin (); M1_iter! = M2.end (); m1_iter++) cout<<" "<< M1_itersecond; cout<<"."<<Endl; //This is the specialized template version of Swapswap (M1, M3); cout<<"After swapping with M3, map M1 is:"; for(M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++) cout<<" "<< M1_itersecond; cout<<"."<<Endl;}6. Map sort problem: The elements in map are automatically sorted by key in ascending order, so you cannot use the sort function on map: For example: #include<map>#include<iostream>using namespacestd;intMain () {map<int,int>M1; Map<int,int>:: Iterator m1_iter; M1.insert (Pair<int,int> (1, - ) ); M1.insert (Pair<int,int> (4, + ) ); M1.insert (Pair<int,int> (3, - ) ); M1.insert (Pair<int,int> (2, - ) ); M1.insert (Pair<int,int> (6, + ) ); M1.insert (Pair<int,int> (7, - ) ); cout<<"The original map M1 is:"<<Endl; for(M1_iter = M1.begin (); M1_iter! = M1.end (); m1_iter++) cout<< m1_iter->first<<" "<<m1_Iter->second<<Endl; } The original map M1 is: 1 - 2 - 3 - 4 + 6 + 7 -Please press any key to continue ...7, the basic operation function of map: C+ + maps is an associative container that contains the keyword/The value "to begin () returns an iterator to the map header clear () deletes all elements count () returns the number of occurrences of the specified element empty () Returns true if Map is empty () returns an iterator pointing to the end of the map Equal_range () returns a special entry for the iterators to erase () to delete an element Find () finds an element Get_allocator () returns a map of the Configurator insert () insertion element Key_comp () returns a function that compares the element key Lower_bound () returns the key value>=the first position of a given element max_size () returns the maximum number of elements that can be accommodated rbegin () returns a reverse iterator to the tail of the map rend () returns a pointer to the map Reverse iterator of the head size () returns the number of elements in a map swap () swap two map upper_bound () return key values>the first position of a given element Value_comp () returns a function that compares the element value
52601624
A detailed map usage in C + + "turn"