[英]Dynamically allocating 2D int array
有人可以在下面的代码中指出我在做什么吗?
int* a = NULL;
int* b = NULL;
a = new int[map->mapSize.width];
b = new int[map->mapSize.height];
layer->tileGids = new int[a][b];
代码使用的是以下内容:
typedef struct _size {
int width, height;
} size;
class Map {
size mapSize;
}
class Layer {
int * tileGids;
}
编辑:编译器错误(在代码的第一位的第6行中):
解:
我决定接受光化学家的回答。 本质上,对我有用的是使用向量而不是数组。 Vector为您管理内存,因此更容易处理。
您不能传递用于初始化数组大小的指针。 现在其他人都提到了这一点。
这篇文章(不是我的)似乎可以为您提供帮助: http : //eli.thegreenplace.net/2003/07/23/allocating-multi-Dimension-arrays-in-c/
您还应该考虑在类Layer的构造函数中进行分配,然后删除其析构函数中的内存(即RAII-资源获取已初始化)。 这被认为是好的风格。
最后,您可以考虑使用连续内存和自定义索引方案,可以轻松使用Layer进行封装。 这当然取决于要取得多大的成就。 它们越大,连续存储的情况就越好。
这应该给你一种味道。
#include <iostream>
#include <cstdlib>
int main()
{
const size_t ROWS = 5;
const size_t COLS = 2;
const size_t size = ROWS*COLS;
int* arr = new int[size];
int i = 0;
for ( size_t r = 0 ; r < ROWS; ++r )
{
for (size_t c = 0; c < COLS; ++c )
{
arr[r*COLS+c] = i++;
}
}
for ( int j = 0; j < i; ++j)
{
std::cout << arr[j] << std::endl;
}
delete [] arr;
}
有人可以在下面的代码中指出我在做什么吗?
很多。 您要分配两个单个数组(“行数组”和“列数组”,而不是您所需要的),然后尝试做一些奇怪的事情。
通常,您(严格地说)不能在C ++中动态分配2D数组(因为类型系统仍需要在编译时就知道类型和尺寸)。 您可以使用大约一个数组的数组来模拟它,但是最好的方法是分配一维数组:
int width=5;
std::vector<int> tab(width*height);
然后通过手动计算坐标来访问元素:
// access the element (1,2)
tab[1 + 2*width] = 10;
这样,您实际上将一维数组解释为二维数组(性能等于静态2D数组)。
然后,为了方便起见,最好用一个类包装索引。 boost::multi_array
也已经为您完成了这项工作。
首先,变量“ a”和“ b”是指针。 您的代码:layer-> tileGids = new int [a] [b]是问题的根本原因。
我正在尝试猜测您的意图,我想您正在尝试做的是layer.tileGids一个2维数组,以引用大小(mapSize.Width,mapSize.height)的“网格”,以便您可以引用网格中的每个“ cell”都使用layer.tileGids [x] [y]。
如果确实要创建二维数组,则有2种方法可以实现。
方法1:
class Layer {
int ** tileGids; // NOTE the "**" to indicate tileGids is a pointer to pointer i.e. 2D array.
}
要初始化它:
int width = map->mapSize.width;
int height = map->mapSize.height;
layer.tileGids = new int*[width]; // NOTE the "int*" to indicate tileGids is a new array of pointers to int.
for (int i = 0; i < width; i++) // Initialize each element in layer.tileGids[] to be a pointer to int.
{
layer.tileGids[i] = new int[height];
}
现在,您可以使用以下命令访问layer.tileGids中的项目:
int value = layer.tileGids[x][y] // where 0 <= x < width and 0 <= y < height
要解除分配该数据结构,类似于分配数据结构,您需要解除分配每个“行”中每个动态分配的数组:
for (int i = 0; i < width; i++)
{
delete [] layer.tileGids[i]; // Deallocate each row.
}
delete [] layer.tileGids; // Deallocate "array" to the pointers itself.
方法2:
现在,另一个更简单,更混乱的方法(避免使用指针)是使用C ++向量类。 您需要进行以下更改:
#include <vector>
class Layer {
vector<vector<int> > tileGids; // Note the space at "<int> >".
}
初始化:
int width = map->mapSize.width;
int height = map->mapSize.height;
layer.tileGids = vector<vector<int> >(width, vector<int>(height, 0)); // Initialize all entries to 0.
要访问元素:
int value = layer.tileGids[x][y]; // Where 0 <= x < width and 0 <= y < height
请注意,对于使用向量的第二种方法,您不必像第一种方法那样执行任何内存清理操作,因为向量将自动处理它。 但是,由于向量可以动态增长,即可以向其中添加项目,因此就失去了使用固定大小的数组的安全性,即,如果使用向量方法,某人可能会意外地增加网格的大小,但是如果他尝试这样做,如果您使用上面的第一种方法将其初始化,则会发生错误,并且您会立即知道出了点问题。
a和b在这里是int*
:
layer->tileGids = new int[a][b];
也许您是想说这个?
layer->tileGids = new int[*a][*b];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.