# Thread: vector of vectors question

1. ## vector of vectors question

I frequently use vectors instead of arrays if I don't know what size it'll be or if I want the user to enter in the size. My question though is what do people usually do when you want to have a two or more dimensional array that you don't know what the size will be until run time?

I've been using vector<vector<blah>> but this is kind of annoying and I was wondering if there was a better/easier way.

2. >>I've been using vector<vector<blah>> but this is kind of annoying and I was wondering if there was a better/easier way.
Write a vector of vectors once and throw it in a matrix class, then use that class when you need a 2d vector :-)

3. So obvious, why didn't I think of that?! Thanks!

4. Actually, one more question about this. Would it be possible to access data directly using []'s? I know how to overload the index operators for something like this:
Code:
myContainer[x];
But I'm not sure how if even possible to overload something to get this:
Code:
myContainer[x][y];
Or am I stuck having to do something like this for my matrix:
Code:
myMatrix.returnValue(x,y);

5. >>Would it be possible to access data directly using []'s?
Consider two classes, a matrix class and an array class. Use the array class with an overloaded [] operator and use that as a return value for the overloaded matrix [] operator :-)
Code:
template<typename T>
class matrix {
public:
class array {
public:
T& operator[](int index);
const T& operator[](int index) const;
...
};

array operator[](int index);
const array operator(int index) const;
...
};
Then you can access it like this
Code:
matrix<int> a(5,10);
...
cout<< a[i][j] <<endl;

6. Nice! Thanks, thats a useful trick to know!