Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

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

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(' '));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't need to create external loop to resize a 2 dimensional vector (matrix). You can simply do the following one line resize() call:

//vector<vector<int>> M;
//int m = number of rows, n = number of columns;
M.resize(m, vector<int>(n));

Hope that helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...