2D array sorting

Hi,I know how to sort a 1D array using function sort().
but if i have a 2D array how do i sort only 1 row of it (or column)?




Thank you very much and i hope you answer soon because have a test tomorrow.
A row of a 2D array is a 1D array, and you can sort it the same way as you'd sort a 1D array: std::sort(std::begin(a[n]), std::end(a[n])); or even std::sort(a[n], a[n] + columns);

Sorting a column with a standard algorithm takes one extra step: to pass it to std::sort(), you need a pair of iterators such that the first iterator points at the element in the first row and the desired column, the second points at the element in the second row and the desired column, etc. With a 2D array, this means that the iterator skips as many elements as there are columns on every iteration. This is called "stride". Strided views are supported directly by std::valarray and by boost::multi_array, but for plain C-style 2D array you'd have to build it. Boost can do it easily: sort( boost::make_iterator_range(&a[0][0] + n, &a[0][0]+C*R) | strided(C) );

full demo: http://liveworkspace.org/code/2In6H2


@Cubbi : Thank you very much .
Topic archived. No new replies allowed.