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:
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:
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 simplystd::vector<T>
, with an appropriate calculation of the index. In this case: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 overloadoperator()
(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()
.