#include <iostream>
#include <algorithm>
#include <functional>
#include <boost/iterator/indirect_iterator.hpp>
template<size_t R, size_t C>
void sort_every_col(int (&a)[R][C])
{
for(size_t c = 0; c < C; ++c)
{
int *tmp[R];
for(size_t r = 0; r < R; ++r)
tmp[r] = &a[r][c];
std::sort(boost::make_indirect_iterator(tmp),
boost::make_indirect_iterator(tmp+R),
std::greater<int>());
}
}
template<size_t R, size_t C>
void print_array(int (&a)[R][C])
{
for(size_t r = 0; r < R; ++r)
{
for(size_t c = 0; c < C; ++c)
std::cout << a[r][c] << ' ';
std::cout << '\n';
}
}
int main()
{
int a2[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
sort_every_col(a2);
print_array(a2);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPGJvb3N0L2l0ZXJhdG9yL2luZGlyZWN0X2l0ZXJhdG9yLmhwcD4KCnRlbXBsYXRlPHNpemVfdCBSLCBzaXplX3QgQz4Kdm9pZCBzb3J0X2V2ZXJ5X2NvbChpbnQgKCZhKVtSXVtDXSkKewogICAgZm9yKHNpemVfdCBjID0gMDsgYyA8IEM7ICsrYykKICAgIHsKICAgICAgICBpbnQgKnRtcFtSXTsKICAgICAgICBmb3Ioc2l6ZV90IHIgPSAwOyByIDwgUjsgKytyKQogICAgICAgICAgICB0bXBbcl0gPSAmYVtyXVtjXTsKICAgICAgICBzdGQ6OnNvcnQoYm9vc3Q6Om1ha2VfaW5kaXJlY3RfaXRlcmF0b3IodG1wKSwKICAgICAgICAgICAgICAgICAgYm9vc3Q6Om1ha2VfaW5kaXJlY3RfaXRlcmF0b3IodG1wK1IpLAogICAgICAgICAgICAgICAgICBzdGQ6OmdyZWF0ZXI8aW50PigpKTsKICAgIH0KfQoKCnRlbXBsYXRlPHNpemVfdCBSLCBzaXplX3QgQz4Kdm9pZCBwcmludF9hcnJheShpbnQgK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