【问题标题】:Arbitrary matrix dimension in matrix class矩阵类中的任意矩阵维数
【发布时间】:2019-10-15 10:40:04
【问题描述】:

任务如下:用组件数据描述“数字矩阵”类:矩阵的维数,指向元素的指针。重载操作:

#include "pch.h"
#include <iostream>
using namespace std;
class Matrix
{
public:
    Matrix()
    {
        int Table[3][3];
    }
    int Table[3][3];
    void Create()
    {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                Table[i][j] = 10;
    }
};
ostream& operator <<(ostream& t, Matrix a)
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
            t << a.Table[i][j] << " ";
        t << "\n";
    }
    return t;
}
Matrix& operator /=(Matrix& a, int num)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            a.Table[i][j] /= num;
    return a;
}
Matrix& operator -(Matrix& a, int empty)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            a.Table[i][j] = -a.Table[i][j];
    return a;
}
Matrix& operator +(Matrix& a, Matrix b)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            a.Table[i][j] += b.Table[i][j];
    return a;
}
int main()
{
    int u;
    setlocale(LC_ALL, "Russian");
    Matrix Example;
    Example.Create();
    Matrix Example1;
    Example1.Create();


    cout << Example;

    cout << Example1;
    cout << "Сумма матриц: "<<endl;
    cout << Example + Example1;

                Example - 1;

                Example1 - 1;


cout<< Example + Example1;

            cout << "На сколько вы хотите её поделить?\n";
            cin >> u;

                Example /= u;

                Example1 /= u;
                cout << Example;

                cout << Example1;


}`

【问题讨论】:

  • 搜索“动态二维数组”应该能找到你需要的东西

标签: c++ class matrix


【解决方案1】:

您需要动态创建矩阵。 为此,您需要使用指针(*)。更改 int 表[3][3] 双桌**;

如何实现的示例(注意我使用矩阵而不是表格)

class Matrix {
private:
    double** matrix;
    int col;
    int row;
public:
    Matrix(){};
    void Create(int row, int col);
};


void Matrix::Create(int row_, int col_){

    double val = 0.0;
    col = col_; // initalize private members
    row = row_;

    matrix = new double*[row]; // Create a new array of size row_

    for(int i = 0; i < row; i++)
    {
        matrix[i] = new double[col]; // Create new cols of size col (inside array of row)
    }

    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            matrix[i][j] = val;
            val = val + 1.0;
        }
    }
}

为了简单起见,我尝试重用您的设计,但我真的建议您尝试在构造函数中指定矩阵的维度,甚至可以在其中构造矩阵。 像这样的:

Matrix(int row_, int col_) : row(row_), col(col_) {*/ create matrix here /*};

如果您愿意,您可以跳过“在此处创建矩阵”部分并使用您自己的 Create()。

【讨论】:

    【解决方案2】:

    您需要为此进行动态内存分配。除非您被明确告知,否则我不会摆弄指针(新建/删除)。作为初学者,您可能应该使用标准模板库 (STL) 工具:

    #include &lt;vector&gt; 并使用std::vector&lt;std::vector&lt;int&gt;&gt; Table 而不是int Table[3][3]。然后像这样写一个构造函数:

    Matrix(std::size_t rows, std::size_t cols)
    {
        Table.resize(rows);
        for (unsigned int i = 0; i < Table.size(); ++i)
            Table[i].resize(cols);
    }
    

    您可以额外存储矩阵的维度,但没有必要这样做,因为您可以从向量中获取信息。用相应的动态大小(存储或从向量中提取)替换所有循环中的硬编码维度。例如:

    
    Matrix& operator +(Matrix& a, Matrix b)
    {
        unsigned int rows = a.Table.size();
        unsigned int cols = a.Table[0].size();
    
        for (unsigned int i = 0; i < rows; i++)
            for (unsigned int j = 0; j < cols; j++)
                a.Table[i][j] += b.Table[i][j];
        return a;
    }
    
    

    但是,这个向量向量并不是真正有效的。最好是单个向量,但我想对于初学者来说还可以。

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 2013-06-23
      相关资源
      最近更新 更多