1

Possible Duplicate:
C++ Overloading : Overloading the [][] operator

I have a class matrix, its data is stored in vector<vector<double> > _data and I want to overload the [][] operator to return _data[row][cols]

I was looking around but couldn't quite understand how to do that using vector of vector

I know I need to overload the [] operator, but it only receives one parameter.

Was thinking of maybe creating an inner helper class but I can't quite get my head around this one.

Any ideas?

Thanks

Community
  • 1
  • 1
La bla bla
  • 8,558
  • 13
  • 60
  • 109

3 Answers3

4

You have at least two choices:

  1. Create a custom MatrixRow class which stores a single row of your matrix. This class can overload operator[]() to return an element in the row. Then your matrix class overloads its own operator[]() to return a MatrixRow.

  2. Overload operator[]() in your matrix class to return a vector<double>. Then you get the second operator[]() for free.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

How about:

double& operator()(size_t row, size_t col) {
    return _data[row][col];
}

// ...
m(2,4) = 10.3;

It's not exactly the same but it is almost as convenient syntactically, and it abstracts away the underlying implementation (so if you decide to use only one vector (1D) and calculate the correct indices to make it appear as 2D, it will be easy to do).

Btw: Using nested vectors has additional memory (and lookup) overhead, which you might not want.

lethal-guitar
  • 4,438
  • 1
  • 20
  • 40
-1

From what i see, you are trying to create bi-dimensional vector. So, here is the way you can do it:

vector <vector<double>> _data;

vector <double> vec_d; // temporary helpful vector
double d=NULL;

for(int i=0;i<column_size;i++){
            _data.push_back(vec_d);

            for(int j=0;row_size<l;j++)
                _data[i].push_back(d);
    }

After doing this, you can acess to your data this way:

_data[1][0] = 1.5;
DmitryK
  • 1,333
  • 1
  • 11
  • 18
  • i think that's what he wanted to achive – DmitryK Jan 08 '13 at 23:27
  • I don't even know what this is doing. – Rapptz Jan 08 '13 at 23:28
  • @DmitryKvochkin: OP didn't write anything about the situation that forces him to overload `[]` operator, he just asked how to overload `[][]` operator (which doesn't exist) – LihO Jan 08 '13 at 23:30
  • well, if you read the comments of the question, he replied that it is exactly what he was trying to achive.... – DmitryK Jan 08 '13 at 23:33
  • @DmitryKvochkin: I see now. You should have write something though, it's not clear at all why you wrote here this snippet of code, what it is supposed to do and how it is supposed to help him. Also your initialization is after the vector object is constructed. What if he needs to initialize it within constructor right when the vector object itself is being constructed... the one to blame here is OP for the way he asked the question... he just didn't provide any details about it. – LihO Jan 08 '13 at 23:37
  • @DmitryKvochkin: Also a side note: I didn't downvote, I'm used to comment and help the author to fix his answer rather than humiliate him because of answering it incorrectly. – LihO Jan 08 '13 at 23:39
  • i appreciated you constructive and well explained comment, i am going to edit my answer so it become clear. – DmitryK Jan 08 '13 at 23:41
  • edited, hope i made myself clear – DmitryK Jan 08 '13 at 23:51
  • @Rapptz: hope you get it now... – DmitryK Jan 08 '13 at 23:55