2016-07-07 53 views
3

是否可以使用STL sort在C++中排序矩陣的行?使用STL的C++排序矩陣的行

像指數xy像我們那種一維數組:

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

我怎麼能叫sortint a[100][100],如果我想從x所以排序i-thy

+2

http://stackoverflow.com/questions/20931669/sort-a-2d-array-in的可能的複製-C-使用內建的-functionsor-任何-其他法 –

回答

3

你可以這樣做:

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

+ 1是必要的,因爲結束迭代器必須行向量的最後一個元素之後1個元素。

如果你想整排排序,你可以做到這一點更優雅像

std::sort(std::begin(a[i]), std::end(a[i]));