简体   繁体   English

C ++多维动态数组

[英]C++ multidimensional dynamic array

Let's say I have this to 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? 现在应该不可能访问诸如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;

是的-但是在C / C ++中,它将以grid [y] [x]布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM