Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3433790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:37:10+00:00 2026-05-18T07:37:10+00:00

I am having a tough time getting my head wrapped around how to initialize

  • 0

I am having a tough time getting my head wrapped around how to initialize a vector of vectors.

typedef vector< vector < vector < vector< float > > > > DataContainer;

I want this to conform to

level_1 (2 elements/vectors)
   level_2 (7 elements/vectors)
      level_3 (480 elements/vectors)
         level_4 (31 elements of float)

Addressing the elements isn’t the issue. That should be as simple as something like

dc[0][1][2][3];

The problem is that I need to fill it with data coming in out of order from a file such that successive items need to be placed something like

dc[0][3][230][22];
dc[1][3][110][6]; //...etc

So I need to initialize the V of V beforehand.

Am I psyching myself out or is this as simple as

for 0..1
    for 0..6
        for 0..479
           for 0..30
               dc[i][j][k][l] = 0.0;

It doesn’t seem like that should work. Somehow the top level vectors must be initialized first.

Any help appreciated. I am sure this must be simpler than I am imagining.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:37:10+00:00Added an answer on May 18, 2026 at 7:37 am
    • Please do not use nested vectors if the size of your storage is known ahead of time, i.e. there is a specific reason why e.g. the first index must be of size 6, and will never change. Just use a plain array. Better yet, use boost::array. That way, you get all the benefits of having a plain array (save huge amounts of space when you go multi-dimensional), and the benefits of having a real object instantiation.

    • Please do not use nested vectors if your storage must be rectangular, i.e. you might resize one or more of the dimensions, but every “row” must be the same length at some point. Use boost::multi_array. That way, you document “this storage is rectangular”, save huge amounts of space and still get the ability to resize, benefits of having a real object, etc.

    The thing about std::vector is that it (a) is meant to be resizable and (b) doesn’t care about its contents in the slightest, as long as they’re of the correct type. This means that if you have a vector<vector<int> >, then all of the “row vectors” must maintain their own separate book-keeping information about how long they are – even if you want to enforce that they’re all the same length. It also means that they all manage separate memory allocations, which hurts performance (cache behaviour), and wastes even more space because of how std::vector reallocates. boost::multi_array is designed with the expectation that you may want to resize it, but won’t be constantly resizing it by appending elements (rows, for a 2-dimensional array / faces, for a 3-dimensional array / etc.) to the end. std::vector is designed to (potentially) waste space to make sure that operation is not slow. boost::multi_array is designed to save space and keep everything neatly organized in memory.

    That said:

    Yes, you do need to do something before you can index into the vector. std::vector will not magically cause the indexes to pop into existence because you want to store something there. However, this is easy to deal with:

    You can default-initialize the vector with the appropriate amount of zeros first, and then replace them, by using the (size_t n, const T& value = T()) constructor. That is,

    std::vector<int> foo(10); // makes a vector of 10 ints, each of which is 0
    

    because a “default-constructed” int has the value 0.

    In your case, we need to specify the size of each dimension, by creating sub-vectors that are of the appropriate size and letting the constructor copy them. This looks like:

    typedef vector<float> d1;
    typedef vector<d1> d2;
    typedef vector<d2> d3;
    typedef vector<d3> d4;
    d4 result(2, d3(7, d2(480, d1(31))));
    

    That is, an unnamed d1 is constructed of size 31, which is used to initialize the default d2, which is used to initialize the default d3, which is used to initialize result.

    There are other approaches, but they’re much clumsier if you just want a bunch of zeroes to start. If you’re going to read the entire data set from a file, though:

    • You can use .push_back() to append to a vector. Make an empty d1 just before the inner-most loop, in which you repeatedly .push_back() to fill it. Just after the loop, you .push_back() the result onto the d2 which you created just before the next-innermost loop, and so on.

    • You can resize a vector beforehand with .resize(), and then index into it normally (up to the amount that you resized to).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a heckuva time with this one, though I feel I'm missing something obvious.
I'm having trouble getting in touch with SQL Server Managemen Studio 2008! I want
Having a problem getting a TreeView control to display node images. The code below
Having programmed through emacs and vi for years and years at this point, I
Having this route: map.foo 'foo/*path', :controller => 'foo', :action => 'index' I have the
Having been a PHP developer on LAMP servers for quite a while, is there
Having worked with Classic ASP for about 2 years now by creating a few
Having read the threads Is SqlCommand.Dispose enough? and Closing and Disposing a WCF Service
Having to upgrade a database schema makes installing a new release of software a
Having integrated merb_auth_password_slice as per the README, I can successfully login as redirect_after_login is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.