Hi there.
I have found how to declare a two dimensional vector like below:

`````` vector < vector < vector<int> > > myvector;
``````

My question is, how do I declare a three dimensional vector? Is it as per below? I just thought there may be a standard for this. Thanks

`````` vector < vector < vector<int> > < vector<int> > > myvector;
``````

## All 6 Replies

No ...

``````vector< vector< vector< int > > > myvector;
``````

Note ... you also could grow new objects ...'step-wise'

as ...

could code a class Matrix using a vector of vectors

then could code a class Cube using a vector < Matrix >

Hi
I think I just clicked. Not that I completely understand what you have said but I think it could mean that I am actually creating a vector of vectors and that second vector can act as the other dimensions which can also be a vector of vectors?

Is that right? But how do you do this? Or, are you saying that a three dimensional vector is impossible?

No ... you can have as many D's as memory space permits.

If you wish to define some new objects, and encapsulate functions and data relevant to those objects, you could start out like this:

``````class Matrix
{
private:
vector< vector< double > > grid;
public:
//def's of ctors and member functions could come here
// ...

} ;

class Cube
{
private:
vector< vector< vector< double > > > box;
public:
//def's of ctors and member functions could come here
// ...

} ;
``````

Or ... maybe some times you might prefer this ...

``````class Cube2
{
private:
vector< Matrix > box;
public:
//def's of ctors and member functions could come here
// ...

} ;
``````

David, thanks for trying to respond but I really don't have the faintest clue what you're telling me :(

Ok ...
firstly you had a typo, I think in your line 1 above:

I have found how to declare a two dimensional vector like below:

``````vector < vector < vector<int> > > myvector;
``````

I think you meant to type (for just a 2D vector):

``````vector< vector< int > > myVec; // this is a 2D int vec //
``````

What you had in that line 1 above, was actually a 3D int vector.

Would you like a little demo program that uses 2D (and then later, 3D) vectors?

Can you start the code?

Thanks David. I'll attempt it and let you know if I have trouble. I understand what you're saying now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.