简体   繁体   中英

How to overload subscript operator for multidimensional vectors?

This is my current Matrix class:

template<class T>
class Matrix
{
    public:
        Matrix() { }
        Matrix(int z, int x, int y) 
        { 
            matrix.resize(z); 
            for (unsigned int i = 0; i < matrix.size(); i++)
                matrix[i].resize(x);

            for (unsigned int i = 0; i < matrix.size(); i++)
            {
                for (unsigned int j = 0; j < matrix[i].size(); j++)
                    matrix[i][j].resize(y);
            }
            Fill(0);
        }

        int dim1() { return matrix.size(); }
        int dim2() { return matrix[0].size(); }
        int dim3() { return matrix[0][0].size(); }

        void Fill(int n);
        bool Set(int z, int x, int y, T value);

        class Row
        {
            std::vector<std::vector<T>> m_row;
            public:
                Row(std::vector<std::vector<T>> row) : m_row(row) { }
                class Column
                {
                    std::vector<T> m_column;
                    public:
                        Column(std::vector<T> column) : m_column(column) { }
                        T& operator [] (int index) { return this->m_column[index]; }
                };
                Column operator [] (int index) { return Column(m_row[index]); }
        };

        Row operator [] (int index) { return Row(matrix[index]); }

    private:
        std::vector<std::vector<std::vector<T>>> matrix;
};

template<class T>
void Matrix<T>::Fill(int n)
{
    for (unsigned int i = 0; i < matrix.size(); i++)
        for (unsigned int j = 0; j < matrix[0].size(); j++)
            for (unsigned int k = 0; k < matrix[0][0].size(); k++)
                matrix[i][j][k] = n;
}

template<class T>
bool Matrix<T>::Set(int z, int x, int y, T value)
{
    if (z < matrix.size() && x < matrix[0].size() && y < matrix[0][0].size())
    {
        matrix[z][x][y] = value;
        return true;
    }
    else
        return false;
}

everything works, except the one line commented out in the code below

Matrix<int> m(3, 10, 20);

m.Set(2, 4, 10, 42);

// m[2][4][10] = 42;

std::cout << "base layer:  " << m.dim1() << std::endl;
std::cout << "layer x:    " << m.dim2() << std::endl;
std::cout << "layer y:    " << m.dim3() << std::endl;
std::cout << "\nm[2][4][10] = " << m[2][4][10] << std::endl;

when I do "m[2][4][10] = 42", instead of the "Set(x, x, x, x)" function, the "cout << m[2][4][10]" returns 0 instead of 42. It just doesn't make sense to me and I would really like to use the subscripting to set values.

Edit : I changed the question title to make a bit more sense.

 operator [] (int index) { return ; }

The return type here designates a function that returns by-value . Thus, a copy of matrix[index] is returned and any subsequent operations done on the return value only affect the copy but not the original object.

Invariably, the solution is to return an lvalue-reference. But that still won't help because of how you're returning the index. Row(...) constructs a temporary instance of Row which is separate from the Row object you should be returning - it is actually a copy of it. There's no need to use this syntax, use return directly:

 operator [] (int index) { return ; }

Note that this not only applies to this function but the one that returns Column as well.

I looked at my code again today and realized all I need is the reference to go both ways. So I changed the Row and the Column variables to references, tested it and it worked.

class Row
{
    std::vector<std::vector<T>>& m_row;
    public:
        Row(std::vector<std::vector<T>>& row) : m_row(row) { }
        class Column
        {
            std::vector<T>& m_column;
            public:
                Column(std::vector<T>& column) : m_column(column) { }
                T& operator [] (int index) { return this->m_column[index]; }
        };
        Column operator [] (int index) { return Column(m_row[index]); }
};

Row operator [] (int index) { return Row(matrix[index]); }

I didn't want to use pointers to avoid memory leeks, which was my initial solution and probably a bad one. I think this should be a good subscript overload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM