fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. struct Pair {
  5. int first; int second;
  6. };
  7. bool compare(const Pair &p1, const Pair &p2) { return p1.first < p2.first; }
  8.  
  9. int main() {
  10. int N=5;
  11. int data[10]= {1,2,7,8,13,14,10,11,4,5};
  12. Pair *pairs = (Pair *)data;
  13.  
  14. std::cout << "unsorted" << std::endl;
  15. for(int i=0; i<N; ++i)
  16. std::cout << i << ": (" << pairs[i].first << ", " << pairs[i].second << ")" << std::endl;
  17.  
  18. std::sort(pairs, pairs+N, compare);
  19.  
  20. std::cout << "sorted" << std::endl;
  21. for(int i=0; i<N; ++i)
  22. std::cout << i << ": (" << pairs[i].first << ", " << pairs[i].second << ")" << std::endl;
  23. return 0;
  24. }
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
unsorted
0: (1, 2)
1: (7, 8)
2: (13, 14)
3: (10, 11)
4: (4, 5)
sorted
0: (1, 2)
1: (4, 5)
2: (7, 8)
3: (10, 11)
4: (13, 14)