为了账号安全,请及时绑定邮箱和手机立即绑定

正在回答

1 回答

这个是什么,没明白

0 回复 有任何疑惑可以回复我~
class Mat

{
public:
    int row = 0;
    int col = 0;
    float * * mat = nullptr;

private:
    void init(int row, int col)
    {
        if (row && col) {
            mat = new float*[row];
            for (int i = 0; i < row; i++) {
                mat[i] = new float[col];
                for (int j = 0; j < col; j++){
                    mat[i][j] = 0;
                    if(i == j){
                        mat[i][j] = 1;
                    }
                }
            }
        }
    }

public:
    Mat(int row = 0, int col = 0)
    {
        this->row = row;
        this->col = col;

        init(row, col);
    }

    Mat(const Mat &m)
    {
        this->row = m.row;
        this->col = m.col;

        init(row, col);
        for (int i = 0; i < row; i++){
            for (int j = 0; j < col; j++){
                mat[i][j] = m.mat[i][j];
            }
        }
    }

    ~Mat()
    {
        if (mat != nullptr) {
            for (int i = 0; i < row; i++){
                if (mat[i]) {
                    delete[] mat[i];
                    mat[i] = nullptr;
                }
            }
            if (mat){
                delete[] mat;
            }   
            mat = nullptr;
        }
    }

    Mat & operator = (const Mat &m)
    {
        if (mat != nullptr) {
            for (int i = 0; i < row; i++){
                if (mat[i]) {
                    delete[] mat[i];
                    mat[i] = nullptr;
                }
            }
            if (mat){
                delete[] mat;
            }
            mat = nullptr;
        }

        row = m.row;
        col = m.col;

        init(row, col);
        for (int i = 0; i < row; i++){
            for (int j = 0; j < col; j++){
                mat[i][j] = m.mat[i][j];
            }
        }

        return *this;
    }

    Mat operator * (const Mat &m)
    {
        EyerMat res(row, m.col);
        
        for (int i = 0; i < res.row; i++) {
            for (int j = 0; j < res.col; j++) {
                res.mat[i][j] = 0.0f;
            }
        }

        if (m.row != col){

        }
        else {
            for (int i = 0; i < res.row; i++) {
                for (int j = 0; j < res.col; j++) {
                    for (int k = 0; k < res.row; k++) {
                        res.mat[i][j] += mat[i][k] * m.mat[k][j];
                    }
                }
            }
        }
            
        return res;
    }
}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
趣味 C++ 进阶
  • 参与学习       13167    人
  • 解答问题       41    个

本课程是 C++ 的进阶课程,继续趣味学习之旅,带你探索 C++ 的高级用法。

进入课程
意见反馈 帮助中心 APP下载
官方微信