vector of vector of vectors

Discussion in 'C++' started by Creighton Hogg, Jan 20, 2006.

  1. 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?
    Creighton Hogg, Jan 20, 2006
    #1
    1. Advertising

  2. Creighton Hogg wrote:
    > 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
    Victor Bazarov, Jan 20, 2006
    #2
    1. Advertising

  3. Creighton Hogg

    Mike Wahler Guest

    "Creighton Hogg" <> wrote in message
    news:p...
    > 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
    Mike Wahler, Jan 20, 2006
    #3
  4. Creighton Hogg

    Guest

    Creighton Hogg wrote:
    > 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.
    , Jan 20, 2006
    #4
  5. Creighton Hogg

    Daniel T. Guest

    In article <>,
    Creighton Hogg <> wrote:

    > 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

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Jan 21, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cheeser

    Sizing a vector of vectors

    cheeser, Sep 28, 2003, in forum: C++
    Replies:
    3
    Views:
    350
    cheeser
    Sep 28, 2003
  2. Nancy Keuss

    vector of vectors

    Nancy Keuss, Jan 13, 2004, in forum: C++
    Replies:
    9
    Views:
    3,051
    Nick Hounsome
    Jan 14, 2004
  3. Replies:
    8
    Views:
    1,894
    Csaba
    Feb 18, 2006
  4. Replies:
    3
    Views:
    680
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    427
    Guest
    Sep 14, 2005
Loading...

Share This Page