Allocate space to a double vector vector vector

Asked

Viewed 79 times

0

I’m trying to allocate space in memory for a double vector vector vector. I know that for a vector vector I can do

vector<vector<double>>  Vetor(51, vector<double>(47, 1))

And this generates me a vector[51][47] but in the case of vector vector this definition is not so clear. Is there any possibility of allocation for this other case?

  • Man, I don’t quite understand your doubt... you want to do this -> vector<vector<vector<double>>>? I do not know what is your class Vector... And vector has dynamic allocation, you want to define an initial size, for each dimension?

  • this, I need to make a vector<vector<vector<double>> name and for this I need to allocate space in memory as I did in the main text with a vector<vector<double>>. because allocating space in memory I can feed name[i][j][k] without using.

1 answer

1


You could do it this way:

vector<vector<vector<double>>> v(WIDTH);
for (int i = 0; i < WIDTH; i++)
    v[i].resize(HEIGHT);
    for (int j = 0; j < HEIGHT; j++)
        v[i][j].resize(DEPTH);

So your vector would be v[WIDTH][HEIGHT][DEPTH]. It is an option that perhaps becomes clearer than nesting everything in several parentheses...

Another option, similar, would be instead of using the method resize, use the method reserve to specify the initial capacity of each dimension.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.