参考别人的写的矩阵的模板
目前暂时只实现赋值,加,减, 乘, 转置;
/************************************
矩阵模板~~=+-*
************************************/
# include<iostream>
using namespace std;
template<typename T>
class Matrix{
private:
int col;
int row;
T ** element;
public: Matrix();
Matrix(const Matrix &);
Matrix(const int, const int);
~Matrix();
void setMatrix(int , int);
Matrix<T> operator = (const Matrix<T> &);
Matrix<T> operator + (const Matrix<T> &);
Matrix<T> operator - (const Matrix<T> &);
Matrix<T> operator * (const Matrix<T> &);
Matrix<T> transpose();
void setM();
void print();
Matrix<T> quickpow(int n);
};
template<typename T>
Matrix<T>::Matrix()
{
row = 0;
col = 0;
element = NULL;
}
template<typename T>
Matrix<T>::Matrix(const Matrix & temp)
{
row = temp.row;
col = temp.col;
element = new T*[row];
for(int k=0; k<row; k++)
element[k] = new T[col];
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
element[i][j] = temp.element[i][j];
}
template<typename T>
Matrix<T>::Matrix(const int m_row, const int m_col)
{
row = col =0;
element = NULL;
setMatrix(m_row, m_col);
}
template<typename T>
void Matrix<T>::setMatrix(int m_row, int m_col)
{
for(int y=0; y<row; y++)
delete [] element[y];
delete []element;
row = m_row;
col = m_col;
element = new T*[row];
for(int i=0; i<row; i++)
element[i] = new T[col];
for(int temprow=0; temprow<row; temprow++)
for(int tempcol=0; tempcol<col; tempcol++)
element[temprow][tempcol]=0;
}
template<typename T>
void Matrix<T>::setM( )
{
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
cin>>element[i][j];
}
template<typename T>
Matrix<T>::~Matrix(){
for(int y=0; y<row; y++)
delete [] element[y];
delete [] element;
}
template<typename T>
Matrix<T> Matrix<T>::operator = (const Matrix<T> & right)
{
for(int p=0; p<row; p++)
delete []element[p];
delete []element;
row = right.row;
col = right.col;
element = new T * [row];
for(int i=0; i<row; i++)
element[i]=new T[col];
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
element[i][j] = right.element[i][j];
return *this;
}
template<typename T>
Matrix<T> Matrix<T>::operator + (const Matrix<T> & right)
{
if(row!=right.row || col !=right.col)
throw string("error");
Matrix<T> result(row, col);
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
result.element[i][j] = element[i][j] + right.element[i][j];
return result;
}
template<typename T>
Matrix<T> Matrix<T>::operator - (const Matrix<T> & right)
{
if(row!=right.row || col !=right.col)
throw string("error");
Matrix<T> result(row, col);
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
result.element[i][j] = element[i][j] - right.element[i][j];
return result;
}
template<typename T>
Matrix<T> Matrix<T>::operator * (const Matrix<T> & right)
{
if(col != right.row)
throw string("error");
Matrix<T> result(row, right.col);
for(int i=0; i<row; i++)
for(int j=0; j<right.col; j++)
{
result.element[i][j]=0;
for(int k=0; k<col; k++)
result.element[i][j]+=element[i][k]*right.element[k][j];
}
return result;
}
template<typename T>
Matrix<T> Matrix<T>::transpose()
{
Matrix<T> result(col, row);
for(int i=0; i<result.row; i++)
for(int j=0; j<result.col; j++)
result.element[i][j] = element[j][i];
return result;
}
template<typename T>
Matrix<T> Matrix<T>::quickpow(int n)
{
Matrix<T> a(*this);
Matrix<T> result(row, col);
for(int i=0; i<result.row; i++)
for(int j=0; j<result.col; j++)
if(i==j) result.element[i][j]=1;
while(n)
{
if(n%2==1){
result= a*result;
}
n=n>>1;
a = a*a;
}
return result;
}
template<typename T>
void Matrix<T>::print()
{
for(int i=0; i<row; i++){
for(int j=0; j<col; j++)
cout<<element[i][j]<<" ";
cout<<endl;
}
}
int main()
{
Matrix<int> a(3, 3);
a.setM();
Matrix<int> b(3, 4);
b.setM();
b = a*b;
b.print();
return 0;
}