fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <boost/iterator/indirect_iterator.hpp>
  5.  
  6. template<size_t R, size_t C>
  7. void sort_every_col(int (&a)[R][C])
  8. {
  9. for(size_t c = 0; c < C; ++c)
  10. {
  11. int *tmp[R];
  12. for(size_t r = 0; r < R; ++r)
  13. tmp[r] = &a[r][c];
  14. std::sort(boost::make_indirect_iterator(tmp),
  15. boost::make_indirect_iterator(tmp+R),
  16. std::greater<int>());
  17. }
  18. }
  19.  
  20.  
  21. template<size_t R, size_t C>
  22. void print_array(int (&a)[R][C])
  23. {
  24. for(size_t r = 0; r < R; ++r)
  25. {
  26. for(size_t c = 0; c < C; ++c)
  27. std::cout << a[r][c] << ' ';
  28. std::cout << '\n';
  29. }
  30. }
  31.  
  32.  
  33. int main()
  34. {
  35. int a2[3][3] = {{1, 2, 3},
  36. {4, 5, 6},
  37. {7, 8, 9}};
  38.  
  39. sort_every_col(a2);
  40. print_array(a2);
  41. }
  42.  
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
7 8 9 
4 5 6 
1 2 3