fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. pair<int, double> p;
  10. vector<pair<int, double> > vp;
  11.  
  12. p.first = 2;
  13. p.second = 2.4;
  14. vp.push_back(p);
  15.  
  16. p.first = 9;
  17. p.second = 3.0;
  18. vp.push_back(p);
  19.  
  20. p.first = 10;
  21. p.second = 3.1;
  22. vp.push_back(p);
  23.  
  24. p.first = 1;
  25. p.second = 2.4;
  26. vp.push_back(p);
  27.  
  28. p.first = 5;
  29. p.second = 3.1;
  30. vp.push_back(p);
  31.  
  32. cout << "before sort:" << endl;
  33. for(auto &p : vp) {
  34. cout << p.first << ", " << p.second << endl;
  35. }
  36.  
  37. sort(vp.begin(), vp.end()/*,
  38. [](const pair<int, double> &p1, const pair<int, double> &p2){
  39.   if (p1.second > p2.second) return true;
  40.   if (p1.second < p2.second) return false;
  41. return p1.first < p2.first;
  42.   }*/
  43. );
  44.  
  45. cout << endl;
  46. cout << "after sort:" << endl;
  47.  
  48. for(auto &p : vp) {
  49. cout << p.first << ", " << p.second << endl;
  50. }
  51. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
before sort:
2, 2.4
9, 3
10, 3.1
1, 2.4
5, 3.1

after sort:
1, 2.4
2, 2.4
5, 3.1
9, 3
10, 3.1