I want to create an operator [] in the case of two dimensional vector. After searching, I have found that it's impossible to pass two arguments. How I can obtain the value of m_matrix[i][j]
in the main?
relevant code:
class MyMatrix
{
public: // Methods
MyMatrix();
~MyMatrix();
int operator[] (int n);
private: // Attributes
int m_n;
int m_m;
std:: vector <std:: vector <int> > m_matrix;
};
int MyMatrix::operator[](int n, int m) // in the cpp
{
if (n>=0 && m>=0 && n<=m_n && m<=m_m)
{
return m_matrix[n-1][m-1];
}
else
{ cout<<"******************"<<endl;
cout<<"No valid index"<<endl;
cout<<"******************"<<endl;
return 0;
}
}
...
mat_test1[2][2]; // for example in the main
What's wrong with this?
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);
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 ()
.
来源:https://stackoverflow.com/questions/27656251/operator-in-two-dimensional-vector