SymmetricMatrix.hpp
#pragma once
#include<iostream>
using namespace std;
template<class T>
class SymmetricMatrix{
public:
SymmetricMatrix(int arry[][3],size_t n){
_arrySize = n*(n + 1) / 2;
_arry = new int(_arrySize);
size_t index = 0;
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
if (i >= j){
_arry[index++] = arry[i][j];
}
}
}
}
T& Access(int row, int col){
if (row < col){
swap(row, col);
}
return _arry[row*(row+1)/2+col];
}
private:
T* _arry;
size_t _arrySize;
size_t _matrixLen;
};
void test(){
int arr[3][3] = { { 0, 1, 2 },{ 1, 0, 2 },{ 2, 1, 0 } };
SymmetricMatrix<int> sm(arr,3);
cout << sm.Access(1, 0) << endl;
}
main.cpp
#include<iostream>
#include"SymmetricMatrix.hpp"
using namespace std;
int main(){
test();
return 0;
}
转载于:https://blog.51cto.com/molova/1709583