How can I resize a 2D vector of objects given the width and height?

c++, c++11, multidimensional-array, resize, vector

Solution

You have to resize the outer and inner vectors separately.

myVector.resize(n);
for (int i = 0; i < n; ++i)
    myVector[i].resize(m);

Problem

My class, `GameBoard`, has a member variable that is a 2D vector of an object of the class `Tile`. The `GameBoard` constructor takes width and height as parameters. How can I get the 2D vector of `Tile` objects to resize according to the width and height passed to the constructor? How can I fill it with `Tile` objects so that I can do something like this? ``` myVector[i][j].getDisplayChar(); ``` Snippet ``` m_vvTiles.resize(iHeight); for(auto it = m_vvTiles.begin(); it != m_vvTiles.end(); it++ ){ (*it).resize(iWidth,Tile(' ')); } ```

Original source