fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. bool operator<(
  8. const tuple<int, int>& t1,
  9. const tuple<int, int>& t2
  10. ) {
  11. return get<1>(t1) > get<1>(t2);// `>` so that it gets sorted in reverse
  12. }
  13.  
  14. int main() {
  15. vector<tuple<int, int>> v;
  16. for (int i = 0; i < 10; ++i) {
  17. v.push_back(make_tuple(0, i));
  18. }
  19. cout << "before sort: ";
  20. for (auto& x : v) { cout << get<1>(x) << ", "; }
  21. cout << endl;
  22.  
  23. auto v2 = v;
  24. sort(v2.begin(), v2.end());
  25. cout << "after sort(begin, end): ";
  26. for (auto& x : v2) { cout << get<1>(x) << ", "; }
  27. cout << endl;
  28.  
  29. sort(v.begin(), v.end(), [](auto& t1, auto& t2) {
  30. return get<1>(t1) > get<1>(t2);// `>` so that it gets sorted in reverse
  31. });
  32. cout << "after sort(begin, end, comparator): ";
  33. for (auto& x : v) { cout << get<1>(x) << ", "; }
  34. cout << endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
before sort: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
after sort(begin, end): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
after sort(begin, end, comparator): 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,