I was trying to to implement a quick Matrix class ( Complete with Eigen values and fast exponention and inverses). As you might guess this consisted a lot of operator overloading , am now trying to overload the []
such that for
matrix A(3,3);
A[i][j] // A[i][j] returns the matrix element at ith row and jth collumn in matrix
This is what i have so far
class matrix
{
public:
int row,col;
vector< vector<long long int> > a;
matrix(int x,int y)
{
row = x;
col = y;
a.resize(x);
for(int i=0;i<x;i++)
a[i].resize(y);
}
vector<long long int>& operator[](int i)
{
return a[i];
}
matrix operator*(const matrix &M)
{
matrix __A(row,M.col);
for(int i=0;i<row;i++)
for(int j=0;j<row;j++)
for(int k=0;k<M.col;k++)
__A.a[i][j] += (M.a[k][j] * a[i][k]);
return __A;
}
};
However Here the A[i] returns a vector<long long int>
and then further i do A[i][j] it should give long long int
. It kinda works all right , but i not satisfied , how do i make A[i][j] return a "%lld"
directly ?
i tried
long long int& operator[][](int i, int j)
{ return a[i][j]; }
But of'course,it didn't worked for me. How do overload the operator to get the value directly? (Also any tip for improving current class is welcome :) )