C++运用move构造matrix类

PROBLEM:

matrix.h + matrix.cpp

   Define a class Matrix to implement a concept – matrix of integers.

The numbers of rows and columns are provided by the user of the class. In addition, define an exception type Matrix_exception , which is the inner class of Matrix.

NOTE:  Implementation Constraint:

When choosing data representation, you cannot use any STL containers. Use low-level array, linked list, and others.
dot: move(),friend
CODE:

matrix.cpp

#include<string.h>
#include <bits/stdc++.h>
#include <algorithm>
#include "matrix.h"
using namespace std;
Matrix::Matrix(int m=0,int n=0)
{
    a=new int [m*n];
    r=m;c=n;
}

Matrix::Matrix(const Matrix& b)
{
    cout<<"COPY"<<endl;
    r=b.r;
    c=b.c;
    _init_a(b.a);
}

Matrix::Matrix(Matrix&& b)
{
    cout<<"MOVE COPY"<<endl;
    r=b.r;c=b.c;
    a=b.a;
    b.r=0;b.c=0;
    b.a=nullptr;
}

Matrix::Matrix()
{
    r=0;c=0;
    a=new int[0];
}

Matrix::~Matrix()
{
    delete [] a;
}


int& Matrix::operator ()(int x,int y)
{
    return a[x*c+y];
}

int Matrix::col()
{
    return c;
}

int Matrix::row()
{
    return r;
}


Matrix Matrix::operator+(Matrix& b)
{
    if(r!=b.r||c!=b.c) throw Matrix_exception{};
    Matrix result{r,c};
    for(int i=0;i<r*c;i++)
    {
        result.a[i]=a[i]+b.a[i];
    }
    return move(result);
}

Matrix Matrix::operator*(Matrix& b)
{
    if (c!=b.r) throw Matrix_exception{};
    Matrix result{r,b.c};
    for (int i=0;i<r;i++)
    {
        for(int j=0;j<b.c;j++)
        {
            int p=0;
            for (int k=0;k<c;k++)
            {
                p+=a[i*c+k]*b.a[k*b.c+j];
            }
            result.a[i*result.c+j]=p;
        }
    }
    return move(result);
}

Matrix& Matrix::operator=(const Matrix& b)
{
    cout<<"COPY ASSIGN"<<endl;
    r=b.r;c=b.c;
    delete [] a;
    a=new int[r*c];
    for (int i=0;i<r*c;i++) a[i]= b.a[i];
    return *this;
}


Matrix& Matrix::operator=(Matrix&& b)
{
    cout<<"MOVE ASSGIN"<<endl;
    if(this!=&b)
    {
//        if(a) delete [] a;
        r=b.r;
        c=b.c;
        a=b.a;
        b.r=0;b.c=0;
        b.a=nullptr;
    }
    return *this;
}


ostream& operator<<(ostream& out,Matrix& b)

{
    for(int i=0;i<b.r;i++)
    {
        for(int j =0;j<b.c;j++)
        {
            out<<b.a[i*b.c+j]<<(j==b.c-1?'\n':' ');
        }
    }
    return out;
}

istream& operator>>(istream& in,const Matrix& b)
{
    for(int i=0;i<b.r;i++)
    {
        for(int j=0;j<b.c;j++)
            in>>b.a[i*b.c+j];
    }
    return in;
}
 

matrix.h

#ifndef RINGBUFFERQUEUE_H_INCLUDED
#define RINGBUFFERQUEUE_H_INCLUDED
#define MAX_N 100001
#include <string>
#include <bits/stdc++.h>
using namespace std;
class Matrix
{
private:
    int * a,r,c;      //r:行 c:列
    friend ostream& operator<<(ostream&,Matrix&);
    friend istream& operator>>(istream&,const Matrix&);


    void _init_a(const int* b)
    {
        a = new int[r*c];
        for(int i=0;i<r*c;i++)
            a[i]=b[i];
    }
public:
    Matrix(int,int);
    ~Matrix();
    Matrix (Matrix&&);
    Matrix (const Matrix&);
    Matrix();
    int col();
    int row();
    int& operator()(int,int);
    Matrix operator*(Matrix&);
    Matrix operator+(Matrix&);
    Matrix& operator=(const Matrix&);
    Matrix& operator=(Matrix&&);
    struct Matrix_exception{};
};


#endif // RINGBUFFERQUEUE_H_INCLUDED


 

testMatrix.cpp

 // test program for Matrix:  testMatrix.cpp
#include <iostream>
using namespace std;

#include "matrix.h"

int main(int argc,char**argv)
try
{
    int m,n;
    cout<<"Input the numbers of rows and columns:"<<endl;
    cin>>m>>n;

    Matrix a{m,n},b{a},c;

    cout<<"Input the elements of Matrix:"<<endl;
    for(int i=0; i<m; i++)
        for(int j=0; j<n; j++)
            cin>>a(i,j);
    cout<<"Input the elements of another Matrix:"<<endl;
    for(int i=0; i<m; i++)
        for(int j=0; j<n; j++)
            cin>>b(i,j);
    cout<<endl;

    cout<<"Matrix A:";
    cout << "rows: " << a.row() << " columns: "
         << a.col() << endl << "data: " << endl << a << endl;
    cout<<"Matrix B:";
    cout << "rows: " << b.row() << " columns: "
         << b.col() << endl << "data: " << endl << b << endl;

    c=a+b;

    cout<<"Matrix A+B:";
    cout << "rows: " << c.row() << " columns: "
         << c.col() << endl << "data: " << endl << c << endl;

    c(0,0)*=100;
    c(1,1)=10000;
    cout << endl;
    cout<<"Matrix C:";
    cout << "rows: " << c.row() << " columns: "
         << c.col() << endl << "data: " << endl << c << endl;

    c=a*b;
    cout<<"Matrix A * B:";
    cout << "rows: " << c.row() << " columns: "
         << c.col() << endl << "data: " << endl << c << endl;

    Matrix d1;
    d1=c;

    Matrix d2{3,4};

    c=d1+d2;

   return 0;
}
catch(Matrix::Matrix_exception)
{
    cerr << "An exception happened in class Matrix"<<endl;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NightHacker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值