vector of vector of vectors

C

Creighton Hogg

Hi,
I'm having a problem that's driving me nuts right now.

I wanted to represent data as a
vector<vector<vector<unsigned short> > >
Now I've been having alot of trouble initializing and
accessing elements for that type.

I can't seem to initialize like blah(10,10,10) to make each
vector length 10, so instead I did blah(10,(10,10)) but even
though that compiles I get runtime errors if I ever do
something likeblah.at(i).at(j).at(k), no matter what the
values of i j and k.

Is there a clean way to do what I want it to do or should I
just not try it this way?
 
V

Victor Bazarov

Creighton said:
I'm having a problem that's driving me nuts right now.

I wanted to represent data as a
vector<vector<vector<unsigned short> > >
Now I've been having alot of trouble initializing and
accessing elements for that type.

I can't seem to initialize like blah(10,10,10) to make each
vector length 10, so instead I did blah(10,(10,10)) but even
though that compiles I get runtime errors if I ever do
something likeblah.at(i).at(j).at(k), no matter what the
values of i j and k.

Is there a clean way to do what I want it to do or should I
just not try it this way?

If you want to initialise them, then the syntax is

vector<vector<vector<unsigned short> > >
blah(10, vector<vector<unsigned short> >
(10, vector<unsigned short>(10) ) );

V
 
M

Mike Wahler

Creighton Hogg said:
Hi,
I'm having a problem that's driving me nuts right now.

I wanted to represent data as a
vector<vector<vector<unsigned short> > >
Now I've been having alot of trouble initializing and
accessing elements for that type.

I can't seem to initialize like blah(10,10,10) to make each
vector length 10, so instead I did blah(10,(10,10)) but even
though that compiles I get runtime errors if I ever do
something likeblah.at(i).at(j).at(k), no matter what the
values of i j and k.

Is there a clean way to do what I want it to do or should I
just not try it this way?

#include <vector>
using std::vector;

int main()
{
vector<unsigned short> v(10);
vector<vector<unsigned short> > v2(10, vector<unsigned short>(10));

vector<vector<vector<unsigned short> > > v3
(10, vector<vector<unsigned short> >
(10, vector<unsigned short>(10)));

v3[0][0][0] = 42;
return 0;
}

Some typedefs might make this a bit less 'cluttered'.

-Mike
 
W

wkaras

Creighton said:
Hi,
I'm having a problem that's driving me nuts right now.

I wanted to represent data as a
vector<vector<vector<unsigned short> > >
Now I've been having alot of trouble initializing and
accessing elements for that type.

I can't seem to initialize like blah(10,10,10) to make each
vector length 10, so instead I did blah(10,(10,10)) but even
though that compiles I get runtime errors if I ever do
something likeblah.at(i).at(j).at(k), no matter what the
values of i j and k.

blah(10, (10, 10)) isn't doing what you think/want it to
do. It equivalent to blah(10, 10) . C++ inherited the
"comma operator" from C. Because the parethenses
around (10, 10) are not a function call operator, the
comma is a comma operator. The result of
a comma operator expression A, B is just B.
(The comma operator was mostly used in C
for funtion-like macros, so C++ inline functions
make if fairly obsolete.)

blah(10, 10) is equivalent to
blah(10, vector<vector<unsigned short> >(10)).
So you're allocating 10 vectors of 10 vectors of
zero unsigned shorts, which is why the 'at(k)'
above always throws (since k >= 0).

blah(10, vector<vector<unsigned short> > (10, vector<unsigned
short>(10)))
will I believe do what you want. But you wouldn't
want to use it in an application where execution
time of minimal heap usage was important.
 
D

Daniel T.

Creighton Hogg said:
Hi,
I'm having a problem that's driving me nuts right now.

I wanted to represent data as a
vector<vector<vector<unsigned short> > >
Now I've been having alot of trouble initializing and
accessing elements for that type.

I can't seem to initialize like blah(10,10,10) to make each
vector length 10, so instead I did blah(10,(10,10)) but even
though that compiles I get runtime errors if I ever do
something likeblah.at(i).at(j).at(k), no matter what the
values of i j and k.

Is there a clean way to do what I want it to do or should I
just not try it this way?

I suggest you make a 3D-Array class...

template< typename T >
class array3d {
std::vector< T > mem;
unsigned dim;
public:
array3d( unsigned d ): mem( d*d*d ), dim( d ) { }

T& operator()(unsigned x, unsigned y, unsigned z ) {
return mem[z + d*y + d*d*x];
}

T& at()(unsigned x, unsigned y, unsigned z ) {
if ( x >= dim || y >= dim || z >= dim ) throw std::eek:ut_of_range();
return mem[z + d*y + d*d*x];
}

// other member-functions to taste
};

use it like:

array3d<unsigned short> foo(10);
foo( 2, 6, 4 ) = 45
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top