fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. #define LEN 5
  5.  
  6. using Pair = std::pair<int, int>;
  7.  
  8. void print(Pair p[], int len)
  9. {
  10. for(int i=0; i< len; i++)
  11. {
  12. std::cout<<"{"<<p[i].first<<","<<p[i].second<<"} | ";
  13. }
  14. std::cout<<std::endl;
  15. }
  16.  
  17. int main() {
  18. Pair P[LEN];
  19. for(int i= 0; i<LEN; i++)
  20. {
  21. P[i] = Pair(LEN - i, i);
  22. }
  23. print(P, LEN);
  24. auto&& comparator = [](const Pair& lhs, const Pair& rhs){ return lhs.second > rhs.second;};
  25. std::sort(P, P + LEN, comparator);
  26. print(P, LEN);
  27. return 0;
  28. }
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
{5,0} | {4,1} | {3,2} | {2,3} | {1,4} | 
{1,4} | {2,3} | {3,2} | {4,1} | {5,0} |