//只是簡單的演示一下,這個實際運用視乎不怎麼多,所以java版不再實現
/* 希疏矩陣應用於對數據的壓縮,僅僅保留不爲0的數據 稀疏矩陣的轉置,可以由多種方式,下面演示的稍顯簡單,時間複雜度略高O(n^2) */ //先來一個矩陣 int a[][4] = { 9,0,0,6, 0,4,0,0, 5,0,0,0, 0,0,8,0, }; //來個三元組 template <class T> struct Three { int _row, _col;//存放行列 T value;//存放值 }; class Transform { public: int count=0; Three<int> three[4]; void init(){//將不爲0的數組保存下來 for (int i = 0;i < 4;i++) { for (int j = 0;j < 4;j++) { if (a[i][j] != 0){ three[count]._row = i; three[count]._col = j; three[count].value = a[i][j]; count++; } } } } void tF() {//轉置矩陣 for (int i = 0;i < count - 1;i++) { int col = three[i]._col; three[i]._col = three[i]._row; three[i]._row = col; } } }; int main() { Transform *form = new Transform(); form->init(); printf("row %d,col %d\n", form->three[1]._row, form->three[1]._col); form->tF(); printf("row %d,col %d", form->three[1]._row, form->three[1]._col); while (1); return 0; }