开发者

C++ multidimensional dynamic array

Let's say I have this to 开发者_StackOverflow社区create a multidimensional array dynamically:

int* *grid = new int*[gridSizeX];

for (int i=0; i<gridSizeX; i++) {
  grid[i] = new int[gridSizeY];
}

Shouldn't be possible now to access elements like grid[x][y] = 20?


Yes, this should work fine.

But... you might want to consider using standard containers instead of manually managing memory:

typedef std::vector<int> IntVec;
typedef std::vector<IntVec> IntGrid;
IntGrid grid(gridSizeX, IntVec(gridSizeY));

grid[0][0] = 20;


Yes - but in C/C++ it will be laid out as grid[y][x].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜