矩阵模板(尚未完善)

参考别人的写的矩阵的模板

目前暂时只实现赋值,加,减, 乘, 转置;



/************************************
矩阵模板~~=+-*

************************************/

# 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值