Map is a standard C + + container, she provides a very good one-to-one relationship, in some programs to build a map can play a multiplier effect, summed up some map basic simple and practical operation!
1. Map the most basic constructor function;
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[112]= "April";//map the simplest and most common inserts to add!
Find the elements in the 3,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 (112);
if (L_it==maplive.end ())
cout<< "We do not find" <<endl;
else cout<< "Wo find" <<endl;
Deletion of elements in 4,map:
If you delete 112;
Map<int, string >::iterator l_it;;
L_it=maplive.find (112);
if (L_it==maplive.end ())
cout<< "We do not find" <<endl;
else Maplive.erase (L_IT); Delete 112;
Basic operation function of map:
C + + maps is an associative container that contains "keyword/value" pairs
Begin () returns an iterator pointing to the map header
Clear () Delete all elements
COUNT () returns the number of occurrences of the specified element
Empty () returns True if Map is empty
End () returns an iterator pointing to the end of the map
Equal_range () returns an iterator to a special entry
Erase () Delete an element
Find () finds an element
Get_allocator () returns the map's Configurator
Insert () Inserts an element
Key_comp () returns the 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 pointing to the tail of the map
Rend () returns a reverse iterator pointing to the map header
Size () returns the number of elements in the map
Swap () Swap two maps
Upper_bound () returns the key value > the first position of a given element
Value_comp () returns a function that compares the element value
code example:
#include <iostream>#include<map>#include<Set>#include<string>#include<vector>using namespacestd;intMain () {map<string,int>mapfortest; Mapfortest.insert (Pair<string,int> ("Jim", the ) ); Mapfortest.insert (Make_pair ("John", - ) ); mapfortest["Jack"] = the; mapfortest["Lily"] = the; cout<<"Using iterator\n"; Map<string,int>::iterator it =Mapfortest.begin (); while(It! =Mapfortest.end ()) {cout<< It->first <<"="<< It->second <<Endl; It++; } cout<<"Using reverse_iterator\n"; Map<string,int>::reverse_iterator REIT =Mapfortest.rbegin (); while(REITs! =Mapfortest.rend ()) {cout<< Reit->first <<"="<< Reit->second <<Endl; REITs++; } cout<<"Using [] to get the value\n"; cout<<"Lily got the score of"<< mapfortest["Lily"] <<Endl; stringQuery_name; cout<<"Please input the name to query\n"; CIN>>Query_name; It=Mapfortest.find (query_name); if(It = =mapfortest.end ()) cout<<"there is no"<< Query_name <<"In the database\n"; Elsecout<<"Use find ():"<< Query_name <<"got the score of"<< It->second <<Endl; Mapfortest.clear (); Mapfortest.insert (Make_pair ("Tom", the ) ); Mapfortest.insert (Pair<string,int> ("Kevin", the ) ); cout<<"Current size of Mapfortest is:"<< mapfortest.size () <<Endl; return 0;}
The performance is as follows:
C + + Map Quick Start