Allocate space for a double vector vector vector

I am 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 gives me a vector [51] [47] but in the case of vector of vector of Vector this definition is not so clear. Is there any possibility of allocation for this other case?

Author: Patrick Machado, 2018-03-07

1 answers

You could do as follows:

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's an option that might be clearer than nesting everything in multiple parentheses...

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

 1
Author: Leonardo Alves Machado, 2018-03-07 14:30:40