二維數組表格內容ios
爲了提升內存使用效率,壓縮表示數組
壓縮,是將有效的數據保存下來,上述中無效的數據直接進行了拋棄,而現實中,每每會將重複的數據視爲一個有效數據存儲,在上述結構中稍做修改便可實現。spa
#include <iostream>3d
using namespace std; code
void printDepress(int arr[][3])內存
{rem
cout<<"------"it
<<arr[0][0]<<" "io
<<arr[0][1]<<" "效率
<<arr[0][2]<<"----"<<endl;
for (int i=1; i<=arr[0][2]; ++i) {
for (int j=0; j<3; ++j) {
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
int data[5][10] = {
0,0,1,0,0,0,0,0,0,0,
0,0,0,9,0,0,0,0,0,0,
0,0,0,0,0,2,0,0,0,0,
0,0,0,0,3,0,0,0,0,0,
0,0,0,0,0,0,0,6,0,0
};
int count = 0;
int depress[20][3];
depress[0][0] = 5;
depress[0][1] = 10;
for (int row=0; row<5; ++row) {
for (int col=0; col<10; ++col) {
if (data[row][col] != 0) {
++count;
depress[count][0] = row;
depress[count][1] = col;
depress[count][2] = data[row][col];
}
}
}
// 有效數據個數
depress[0][2] = count;
printDepress(depress);
return 0;
}
結果:
------5 10 5----
0 2 1
1 3 9
2 5 2
3 4 3
4 7 6
Program ended with exit code: 0