Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ sort matrix's row using STL

Tags:

c++

sorting

g++

stl

Is it possible to sort matrix's row using STL sort in c++?

like we sort 1-dimensional array from index x to y like:

int a[100];
sort(a+x, a+y+1);

how can I call sort for int a[100][100] if I want so sort i-th row from x to y?

like image 715
Herokiller Avatar asked Jul 15 '26 06:07

Herokiller


1 Answers

You can do:

std::sort(a[i] + x, a[i] + y + 1);

The + 1 is necessary because the end iterator must point 1 element after the last element of the row vector.

If you want to sort the whole row, you can do it more elegantly like

std::sort(std::begin(a[i]), std::end(a[i]));
like image 55
vsoftco Avatar answered Jul 17 '26 20:07

vsoftco