In order to achieve a quick lookup, the map itself is stored sequentially (such as a red-black tree). When we insert <key, value> Key-value pairs, they are stored in the order of key size. Definition of Map:
1 class class class // entry is key 2 // The key value is used for the function object of the key comparison, with the relative greater3class Allocator = allocator<pair< Const 4 // for storage allocation 5 class
Where less is implemented:
1 template <class2struct less:binary_function <t,t,bool3 { 4 booloperator() (constconst Const 5 {return x<y;} 6 };
Sort the key
From the above, sort by key, just customize a class for comparison is OK.
For example:
1 structCmpbykeylength2 { 3 BOOL operator()(Const string& K1,Const string&K2)4 { 5 returnK1.length () <k2.length (); 6 } 7 }; 8 intMain ()9 { Tenmap<string,int, cmpbykeylength>Name_score_map; Onename_score_map["Limin"] = -; AName_score_map.insert (Make_pair ("Bing", About)); -Name_score_map.insert (Make_pair ("Albert", the)); - for(map<string,int>::iterator iter =Name_score_map.begin (); theIter! =Name_score_map.end (); -++iter) - { -cout << *iter <<Endl; + } - + return 0; A}
Sort by value
For value, you can neither press compare nor use sort, because sort is only available for sequence (linear) containers, and map is a collection container (for example: Red black tree)
The element type in map is pair
1Template <classT1,classT2>structpair2 { 3 typedef T1 First_type; 4 typedef T2 Second_type; 5 6T1 first;//corresponding Key7T2 second;//corresponding Value8 9 //constructor FunctionTen pair (): First (T1 ()), second (T2 ()) {} OnePairConstt1& x,Constt2&y): First (x), second (y) {} ATemplate <classUclassV> -Pair (ConstPair<u,v> &p): First (P.first), second (P.second) {} -}
In addition, in the <utility> header file, the < operator is also overloaded for pair
1template<class_T1,class_t2>2InlineBOOL operator< (ConstPAIR<_T1, _t2>& __x,ConstPAIR<_T1, _t2>&__y)3 { 4 return__x.first < __y.first | |//Key Different5(! (__y.first < __x.first) && __x.second < __y.second);//key is the same, the value is compared6 //why not use = =? Because the key of the map must implement the overload of the < operator, as to = = cannot be guaranteed, and the floating-point number ...7}
So it's possible to sort the value with map + vector
For example:
1typedef pair<int,int>PAIR; 2 BOOLCmp_by_value (Constpair& LHS,Constpair&RHS) { 3 returnLhs.second <Rhs.second; 4 } 5 structCmpbyvalue {6 BOOL operator()(Constpair& LHS,Constpair&RHS) { 7 returnLhs.second <Rhs.second; 8 } 9 }; Ten intMain () { Onemap<int,int>MyMap; AMyMap [1] = -; -MyMap [2] = -; -Mymap.insert (Make_pair (3, About)); theMyMap Insert (Make_pair (4, the)); - //dump the elements in the map into a vector -Vector<pair>Name_score_vec (Mymap.begin (), Mymap.end ()); - sort (Mymap.begin (), Mymap.end (), Cmpbyvalue ()); + //sort (Name_score_vec.begin (), Name_score_vec.end (), cmp_by_value); - for(inti =0; I! = Mymap.size (); ++i) { +cout << MyMap [i] <<Endl; A } at return 0; -}
Sort The algorithm is as follows:
1 template <class randomaccessiterator> 2void sort ( Randomaccessiterator First, Randomaccessiterator last); 3 4 Template <classclass compare> 5void
C + + Map