So how do we compare pair by value? The first kind: is the most primitive method, write a comparison function, the second kind: just used, write a function object. Both of these approaches are relatively simple to implement.
- typedef pair<string, int> pair;
- BOOL Cmp_by_value (const pair& LHS, const pair& RHS) {
- return Lhs.second < Rhs.second;
- }
- struct Cmpbyvalue {
- bool Operator () (const pair& LHS, const pair& RHS) {
- return Lhs.second < Rhs.second;
- }
- };
Next, let's look at the sort algorithm, is it also like map, can we specify how to compare between the elements?
- Template <class randomaccessiterator>
- void Sort (randomaccessiterator first, Randomaccessiterator last);
- Template <class Randomaccessiterator, class compare>
- void Sort (randomaccessiterator first, Randomaccessiterator last, Compare comp);
As we can see, it is exciting that the sort algorithm, like map, allows us to specify how the elements are compared, specifying compare. It is important to note that the map is specified at the time of definition, so the parameter is passed directly to the class name of the function object, just like the type name specified when the key and value are specified, the sort algorithm is specified at invocation, it needs to pass in an object, and of course, the class name () calls the constructor to generate the object.
It is also possible to pass in a function pointer, which is the function name of the first method mentioned above. (should be there is a function pointer to the function object conversion, or the two call form is consistent, the exact reason still do not understand, want to know the friend to speak, first thank you. )
"Reference Code"
- int main () {
- Map<string, int> name_score_map;
- name_score_map["limin"] = 90;
- name_score_map["Zilinmi"] = 79;
- name_score_map["BoB"] = 92;
- Name_score_map.insert (Make_pair ("Bing", 99));
- Name_score_map.insert (Make_pair ("Albert", 86));
- //Transfer elements from map to vector
- Vector<pair> Name_score_vec (Name_score_map.begin (), Name_score_map.end ());
- Sort (Name_score_vec.begin (), Name_score_vec.end (), Cmpbyvalue ());
- //Sort (Name_score_vec.begin (), Name_score_vec.end (), cmp_by_value);
- For (int i = 0; I! = Name_score_vec.size (); ++i) {
- cout << Name_score_vec[i] << Endl;
- }
- return 0;
- }
"Run Results"
- #include <iostream>
- #include <cstdlib>
- #include <map>
- #include <vector>
- #include <string>
- #include <algorithm>
- Using namespace std;
- INT cmp (const pair<string, int>& x, const pair<string, int>& y)
- {
- return x.second > Y.second;
- }
- void Sortmapbyvalue (map<string, int>& tmap,vector<pair<string, int> >& tVector)
- {
- for (map<string, int>::iterator curr = Tmap.begin (); Curr! = Tmap.end (); curr++)
- Tvector.push_back (Make_pair (Curr->first, Curr->second));
- Sort (Tvector.begin (), Tvector.end (), CMP);
- }
- int main ()
- {
- Map<string, int> tMap;
- string Word;
- While (CIN >> Word)
- {
- Pair<map<string,int>::iterator,bool> ret = Tmap.insert (Make_pair (Word, 1));
- if (!ret.second)
- ++ret.first->second;
- }
- Vector<pair<string,int>> tvector;
- Sortmapbyvalue (Tmap,tvector);
- For (int i=0;i<tvector.size (); i++)
- cout<<tvector[i].first<<":" <<tVector[i].second<<endl;
- System ("pause");
- return 0;
- }
Sorting by value of map in C + + STL