fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. typedef union {
  5. int data[2];
  6. } Pair;
  7.  
  8. bool compare(Pair i,Pair j) { return i.data[0] < j.data[0]; }
  9.  
  10. int main() {
  11. int N=5;
  12. int data[10] = {1,2, 7,8, 13,14, 4,5, 10,11};
  13. Pair *pairs = (Pair *)((void *)data);
  14.  
  15. std::cout << "unsorted" << std::endl;
  16. for(int i=0; i<N;++i) std::cout << i << ": (" << pairs[i].data[0] << ", " << pairs[i].data[1] << ")" << 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) std::cout << i << ": (" << pairs[i].data[0] << ", " << pairs[i].data[1] << ")" << std::endl;
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
unsorted
0: (1, 2)
1: (7, 8)
2: (13, 14)
3: (4, 5)
4: (10, 11)
sorted
0: (1, 2)
1: (4, 5)
2: (7, 8)
3: (10, 11)
4: (13, 14)