Sorting by value of map in C + + STL

Source: Internet
Author: User

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.

  1. typedef pair<string, int> pair;
  2. BOOL Cmp_by_value (const pair& LHS, const pair& RHS) {
  3. return Lhs.second < Rhs.second;
  4. }
  5. struct Cmpbyvalue {
  6. bool Operator () (const pair& LHS, const pair& RHS) {
  7. return Lhs.second < Rhs.second;
  8. }
  9. };


Next, let's look at the sort algorithm, is it also like map, can we specify how to compare between the elements?

    1. Template <class randomaccessiterator>
    2. void Sort (randomaccessiterator first, Randomaccessiterator last);
    3. Template <class Randomaccessiterator, class compare>
    4. 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"

  1. int main () {
  2. Map<string, int> name_score_map;
  3. name_score_map["limin"] = 90;
  4. name_score_map["Zilinmi"] = 79;
  5. name_score_map["BoB"] = 92;
  6. Name_score_map.insert (Make_pair ("Bing", 99));
  7. Name_score_map.insert (Make_pair ("Albert", 86));
  8. //Transfer elements from map to vector
  9. Vector<pair> Name_score_vec (Name_score_map.begin (), Name_score_map.end ());
  10. Sort (Name_score_vec.begin (), Name_score_vec.end (), Cmpbyvalue ());
  11. //Sort (Name_score_vec.begin (), Name_score_vec.end (), cmp_by_value);
  12. For (int i = 0; I! = Name_score_vec.size (); ++i) {
  13. cout << Name_score_vec[i] << Endl;
  14. }
  15. return 0;
  16. }

"Run Results"

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <map>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7. Using namespace std;
  8. INT cmp (const pair<string, int>& x, const pair<string, int>& y)
  9. {
  10. return x.second > Y.second;
  11. }
  12. void Sortmapbyvalue (map<string, int>& tmap,vector<pair<string, int> >& tVector)
  13. {
  14. for (map<string, int>::iterator curr = Tmap.begin (); Curr! = Tmap.end (); curr++)
  15. Tvector.push_back (Make_pair (Curr->first, Curr->second));
  16. Sort (Tvector.begin (), Tvector.end (), CMP);
  17. }
  18. int main ()
  19. {
  20. Map<string, int> tMap;
  21. string Word;
  22. While (CIN >> Word)
  23. {
  24. Pair<map<string,int>::iterator,bool> ret = Tmap.insert (Make_pair (Word, 1));
  25. if (!ret.second)
  26. ++ret.first->second;
  27. }
  28. Vector<pair<string,int>> tvector;
  29. Sortmapbyvalue (Tmap,tvector);
  30. For (int i=0;i<tvector.size (); i++)
  31. cout<<tvector[i].first<<":" <<tVector[i].second<<endl;
  32. System ("pause");
  33. return 0;
  34. }

Sorting by value of map in C + + STL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.