n维矩阵运算符[]

时间:2016-04-05 05:57:43

标签: c++ operator-overloading

假设我想为类型为T的值的n维矩阵实现一个类:

template <typename T> class Matrix
{
 Matrix(const unsigned &dimensions ); //constructor
 T &operator[](int idx);  //set data operator
 const T &operator[](int idx) const; //get data operator
}

Matrix m(3);
m[0][0][0] = 1;
m[1][2][3] = 4;

如何编写运算符[] []来访问n维矩阵内的数据。 例如:

为二维矩阵,3等编写运算符之间是否存在差异?

编辑1

此问题类似于&#34; Operator[][] overload&#34;,但它涉及更一般的情况,关于n维角色矩阵。

虽然以下答案是正确的: https://stackoverflow.com/a/11109487/4732868

2 个答案:

答案 0 :(得分:1)

你需要这样的东西:

// Matrix.h
#ifndef MATRIX_H
#define MATRIX_H

#include <map>

/**
 * This is the default container class using in Matrix. This can easily
 * be changed through template paramenters of Matrix. See below for more
 * about what the container is used for.
 */
template <typename T>
class DefaultContainer : public std::map<int, T>
{

};

/**
 * Matrix is a class template that implements a multi-dimensional storage
 * of specific type T. The Container is responsible to how these Ts will
 * be stored and retrieved from memory.
 * This is the general case for dimensions > 1. For the simple case of 1
 * dimension see below.
 */
template <typename T,
          int dimensions,
          template <typename> class Container = DefaultContainer
          >
class Matrix
{
    /**
     * A Matrix of n dimensions is actually an array of matrices each has
     * n-1 dimensions.
     * This is what happens here. m_data, in its simple case, is an array
     * of itemTypes each of them is defined as a Matrix of dimensions-1
     */ 
    typedef Matrix<T,dimensions-1, Container> itemType;

    Container<itemType> m_data;

public:
    /**
     * This returns an item of the array m_data which is probably a matrix
     * of less dimensions that can be further accessed be the same operator
     * for resolving another dimension.
     */
    itemType& operator[](int idx)
    {
        return m_data[idx];
    }

    const itemType& operator[](int idx) const
    {
        return m_data[idx];
    }
};

/**
 * This is the simple case of a one-dimensional matrix which is technically
 * an array in its simplest case.
 */
template <typename T,
          template <typename> class Container
          >
class Matrix<T,1,Container>
{
    /**
     * Here we are defining an array of itemType which is defined to be T.
     * so we are actually defining an array of T.
     */
    typedef T itemType;

    Container<itemType> m_data;

public:
    itemType& operator[](int idx)
    {
        return m_data[idx];
    }

    const itemType& operator[](int idx) const
    {
        return m_data[idx];
    }
};

#endif // MATRIX_H

示例用法:

#include <iostream>
#include "matrix.h"

int main(int argc, char *argv[])
{
    Matrix<int, 2> m;

    /**
     * m here is a two-dimensional matrix.
     * m[0] is resolving the first dimension of the matrix. it is like
     * getting the first row which should be an array of items. And this
     * is actually what m[0] returns. It returns a one-dimensional matrix
     * (an array) whose items can also be accessed using operator[] to
     * get one of those items m[0][1].
     */

    m[0][1] = 1;
    m[1][2] = 5;

    std::cout << m[0][1];
    std::cout << m[1][2];

    return 0;
}

答案 1 :(得分:0)

我以非常全面的方式实现了这一点:我将矩阵作为向量的向量(用于2-d)并重载operator[]以返回向量,并且因为向量对象具有重载operator[]它给出了一种错觉,即您正在执行数组中基本上完成的操作(array[int][int])。但是,很难以这种方式指定尺寸。另一个选择是执行所做的事情here(链接由momo提供)并使用operator()(int, int, int, etc...)