Operator [] in two dimensional vector

南笙酒味 提交于 2019-12-06 06:39:58

To resume the comment, you may do:

class MyMatrix
{
public:

    // Other methods

    const std::vector<int>& operator[] (int m) const { return m_matrix.at(m); }
    std::vector<int>& operator[] (int m) { return m_matrix.at(m); }

    int operator () (int m, int n) const { return m_matrix.at(m).at(n); }
    int& operator () (int m, int n) { return m_matrix.at(m).at(n); }

private:
    std::vector<std::vector<int>> m_matrix;
};

Note: I used at instead of [] to use the check from vector and so it throws exception for out of bound access.

And then use it:

MyMatrix matrix(5, 4); // size of the matrix from constructor

matrix[0][1] = 42; // MyMatrix::operator [] followed by std::vector<int>::operator[]
matrix(2, 2) = 42; // MyMatrix::operator () (int, int);

Live example.

There are several ways, but in the end, they all come down to using a proxy.

For various reasons, the usual implementation of a Matrix isn't std::vector<std::vector<T>>, but simply std::vector<T>, with an appropriate calculation of the index. In this case:

class Matrix
{
    int myRows;
    int myColumns;
    std::vector<int> myData;

    class Proxy
    {
        Matrix* myOwner;
        int myRow;
    public:
        Proxy( Matrix* owner, int row )
            : myOwner( owner )
            , myRow( row )
        {
        }
        int& operator[]( int column )
        {
            return myOwner->get( myRow, column );
        }
    };
public:
    Matrix( int rows, int columns )
        : myRows( rows )
        , myColumns( columns )
        , myData( rows * columns )
    {
    }

    int& get( int row, int column )
    {
        assert( 0 <= row && row < myRows
                && 0 <= column && column < myColumns );
        return myData[ row * myColumns + column ];
    }

    Proxy operator[]( int row ) { return Proxy( this, row ); }
};

In fact, you'll want a few more overloads (and possibly two proxies) to handle const and non-const overloads. But this is the general pattern for such things.

Be aware, too, that there are two conventions concerning such overloads. One is to say that the [] returns a projection, and that the correct solution for handling indexing into multi dimensional structures is to overload operator() (which can be made to take an arbitrary number of parameters). I personally prefer the [][] solution, but this preference is not universal; a lot of people with a more mathematical background prefer using ().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!