• 13
• 15
• 19
• 27
• 9

# std

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How can i build a tridimensional array using std? ThanX

##### Share on other sites
Can you elaborate a little more? I am assuming you are talking about building a three-dimensional array in Standard C++. If not please tell me. However, the answer to that would be this:

int threedarray[2][2][2]; //Three dimensional array

##### Share on other sites
<type of objects in array> <name of array>[#][#][#];

##### Share on other sites
I need the form from the standard C++(STD) way

This should be a stack of moves initialized at:
32 players
MAX_LEVELS levels
all the moves at every levels pushed accordingly

vector<vector<vector<unsigned __int64> > >stack;

but i need to initialize the sizes of the first two of them and i don't know how.....[sad]

I also tried :
stack(32,(MAX_LEVEL,std::vector<unsigned __int64>()))
but no luck.....

##### Share on other sites
use some typedef magic to help out:

typedef std::vector<unsigned __int64> vec1d;typedef std::vector< vec1d > vec2d;typedef std::vector< vec2d > vec3d;vec3d stack(32, vec2d(MAX_LEVEL));

You might want to look into the method std::vector::reserve if your gonna go down this root [smile] clickly

Or alternatively check-out Boost.MultiArray

##### Share on other sites
STL is the actual framework which provides "containers", which are the things you're looking for to produce a "tridimensional array"

"std" is just a namespace, in which the various STL containers are placed - along with all the iostream stuff like 'cin' and 'cout' and 'endl', and a bunch of other things too (including anything you might choose to put in there - but doing so is probably a bad idea).

As for doing the actual initialization: The std::vector constructor that you're probably most interested in here is:
vector(size_type n, const T& t) 	 Sequence 	Creates a vector with n copies of t.

So we're going to make a 1-d array with z many copies of your default element, which represents one item in the 'innermost' dimension of the 'array'. Then we make a 2-d array with y-many copies of that 1-d 'prototype', then a 3-d array with x-many copies of *that*:

vector<int> 1dproto(z, 0);vector<vector<int> > 2dproto(y, 1dproto);vector<vector<vector<int> > > arr(x, 2dproto);

You should keep in mind that the resulting structure is more flexible than an ordinary 3-d array in a bunch of ways. It doesn't have to remain rectangular, for example, and it won't use contiguous storage space AFAIK. If you want something that's still constrained to be rectangular (and keeps everything together in a single allocation) but still lets you resize each dimension, you'll probably have to write some wrapper code on top of a single vector.

Or you could just look into boost::multi_array. :)

##### Share on other sites
Quote:
 Original post by snk_kiduse some typedef magic to help out:typedef std::vector vec1d;typedef std::vector< vec1d > vec2d;typedef std::vector< vec2d > vec3d;vec3d stack(32, vec2d(MAX_LEVEL));You might want to look into the method std::vector::reserve if your gonna go down this root [smile] clicklyOr alternatively check-out Boost.MultiArray

I had a friend who has that Library, but I'm confused on how to install it with DevCpp