3

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?

Herokiller
  • 2,891
  • 5
  • 32
  • 50
  • 2
    Possible duplicate of http://stackoverflow.com/questions/20931669/sort-a-2d-array-in-c-using-built-in-functionsor-any-other-method – Alexei Barnes Jul 07 '16 at 11:32

1 Answers1

3

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]));
vsoftco
  • 55,410
  • 12
  • 139
  • 252