我想按列和行对 4X4 矩阵(2D 数组)进行排序。例如:
原始矩阵:
6 2 9 4
4 5 -1 9
4 -2 0 7
3 2 10 3
期望的结果:
-2 -1 0 2
2 3 3 4
4 4 5 6
7 9 9 10
最佳答案
二维数组基本上使用连续的内存位置,因此您可以像对一维数组进行排序一样对其进行排序
const int row = 4,col = 4;
int mat[row][col] = {6 , 2 , 9 , 4
,4 , 5 , -1 , 9
,4 ,-2 , 0 , 7
,3 ,2 , 10 , 3};
sort(&mat[0][0],&mat[0][0]+(row*col));
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<mat[i][j]<<" ";
cout<<endl;
}
关于c - 如何在c中对矩阵(二维数组)进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827891/