重载运算符[]

发布于 2024-10-09 10:51:49 字数 434 浏览 0 评论 0 原文

我有一个任务是在 C++ 中编写一个类矩阵,并且有一个条件,要覆盖矩阵的运算符 [],因此如果我有一个名为 Matrix 且带有“Matrix[0][0]”的矩阵,我必须首先使用它元素,在它的第一行。我用二维动态数组和模板(T **矩阵)表示矩阵。你能帮我一下吗?

PS:我用这个方法来创建二维数组:

template <class T>
T ** Matrix<T>::createMatrix(unsigned int rows, unsigned int cols)
{
    T** matrix = new T*[rows];
    for (unsigned int i = 0; i < rows; i++) {

        matrix[i] = new T[cols];

    }
    return matrix;
}

I have a task to write a class matrix in C++ and there is a condition, to override the operator [] for matrix so if I have a matrix with name Matrix with this "Matrix[0][0]" I must take it's first element, on it's first line. I have represented the matrix with two dimensional dynamic array and templates (T **matrix). Could you help me, please?

PS: This method I'm using to create the two dimensional array:

template <class T>
T ** Matrix<T>::createMatrix(unsigned int rows, unsigned int cols)
{
    T** matrix = new T*[rows];
    for (unsigned int i = 0; i < rows; i++) {

        matrix[i] = new T[cols];

    }
    return matrix;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

森林迷了鹿 2024-10-16 10:51:50

您无法在 C++ 中本地执行此操作,但您可以简单地以这种方式实例化您的矩阵:

Matrix<Matrix<int>> m;

这样第一个 [ ] 返回另一个矩阵,该矩阵将返回您想要的项目。当然,对于您的类来说,更好的名称是 Vector,以及一个名为 Matrix 的包装类,它创建一个内部 Vector>

You can't do that natively in C++, but you could simply instanciate your matrix this way:

Matrix<Matrix<int>> m;

This way the first [ ] returns another matrix which will return the item you want. Of course a much better name for your class then would be Vector, and a wrapper class called Matrix that creates an internal Vector<Vector<T>>.

自在安然 2024-10-16 10:51:50

正如您的要求所述,您用来创建矩阵的方法与类没有任何关系。您正在创建一个简单的 T**,在类方法中执行它并没有什么区别。

创建可在 matrix[i][j] 中使用的 T 矩阵意味着表达式 matrix[i] 必须返回类型上还定义了运算符 [] 返回 T。因此,通常的方法是将其分解为以下步骤:

  • Matrix operator[] (int) 返回一个 MatrixRow&
  • MatrixRow& operator[] (int) 返回一个 T&

当从矩阵读取值时,还会使用这些运算符的 const 版本(非常量写入矩阵时将使用版本)。

因此,至少,您应该创建一个具有此接口的类:

template <typename T>
class Matrix {
    public:
        Matrix(int rows, int cols);

        const MatrixRow<T>& operator[] (int row) const;
        MatrixRow<T>& operator[] (int row);
}

PS:这听起来像一个家庭作业问题,是吗?

The method you are using to create the matrix doesn't have anything to do with a class, as your requirements state. You are creating a simple T**, it doesn't make a difference that you are doing it in a class method.

Creating a matrix of T that can be used as in matrix[i][j] means that the expression matrix[i] must return a type on which operator [] is also defined to return a T. Therefore, the usual way to do it is break it down in steps:

  • Matrix<T> operator[] (int) returns a MatrixRow<T>&
  • MatrixRow<T>& operator[] (int) returns a T&

There will also be const versions of these operators to be used when reading values from the matrix (non-const versions will be used when writing to the matrix).

So, at the minimum, you should create a class with this interface:

template <typename T>
class Matrix {
    public:
        Matrix(int rows, int cols);

        const MatrixRow<T>& operator[] (int row) const;
        MatrixRow<T>& operator[] (int row);
}

PS: This reads like a homework question, is it one?

幽梦紫曦~ 2024-10-16 10:51:49

我假设矩阵是矩阵的 T** 类型的成员变量。

template< class T >
T* operator []( Matrix<T>& m, unsigned int row )
{
  // maybe remember rows and assert(row<rows);
  return m.matrix[ row ];
}

现在您可以编写类似于

Matrix<T> m(50,9999);
m[42][1337]; 

访问第 42 行中的元素 1337 的内容。

i assume matrix is a member variable of type T** of Matrix.

template< class T >
T* operator []( Matrix<T>& m, unsigned int row )
{
  // maybe remember rows and assert(row<rows);
  return m.matrix[ row ];
}

Now you may write something like

Matrix<T> m(50,9999);
m[42][1337]; 

to access element 1337 in row 42.

手长情犹 2024-10-16 10:51:49

您可以通过两个课程来完成此操作。矩阵类重写 [] 并返回一个行对象。 row 对象覆盖 [] 并返回一个标量。

You can do this with two classes. The matrix class overrides [] and returns a row object. The row object overrides [] and returns a scalar.

苏璃陌 2024-10-16 10:51:49

您想要做的是在operator[]中返回一个T*,因为然后它可以将operator[]本机应用于它并获得您想要的结果。然而,我想指出的是,让你的指针保持原始状态是非常糟糕的做法。

What you want to do is return a T* in operator[], as it can then have operator[] applied to it natively and get the result that you want. However, I want to point out that it's pretty bad practice to leave your pointers raw.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文