Allocate space for a vector vector of double

0

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

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

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

    
asked by anonymous 07.03.2018 / 14:30

1 answer

1

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 may be clearer than nesting in multiple parentheses ...

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

    
07.03.2018 / 15:23