我如何声明一个2d数组使用新?
比如,对于一个“普通”数组,我将:
int* ary = new int[Size]
but
int** ary = new int[sizeY][sizeX]
A)不工作/编译b)没有完成什么:
int ary[sizeY][sizeX]
所做的事。
我如何声明一个2d数组使用新?
比如,对于一个“普通”数组,我将:
int* ary = new int[Size]
but
int** ary = new int[sizeY][sizeX]
A)不工作/编译b)没有完成什么:
int ary[sizeY][sizeX]
所做的事。
当前回答
int **arr = new int* [NoOfRows]
for(int i = 0; i<NoOfRows; i++)
arr[i] = new int[noOfColumn];
这是在c++中使用new声明2D数组的好方法 这里,NoOfRow显示了你需要多少行 ans noOfColumn显示你需要多少列
其他回答
从静态数组的例子中,我假设你想要一个矩形数组,而不是锯齿形数组。你可以使用以下方法:
int *ary = new int[sizeX * sizeY];
然后你可以像这样访问元素:
ary[y*sizeX + x]
不要忘记在ary上使用delete[]。
如何分配一个连续的多维数组在GNU c++ ?有一个GNU扩展允许“标准”语法工作。
问题似乎来自操作符new[]。请确保使用operator new:
double (* in)[n][n] = new (double[m][n][n]); // GNU extension
这就是全部:你得到了一个c兼容的多维数组……
尽管这个流行的答案将为您提供所需的索引语法,但它的效率是双重的:在空间和时间上都大而慢。有更好的办法。
为什么答案又大又慢
建议的解决方案是创建一个指针的动态数组,然后将每个指针初始化到它自己的独立动态数组。这种方法的优点是它提供了你习惯的索引语法,所以如果你想找到矩阵在x,y位置的值,你说:
int val = matrix[ x ][ y ];
这是因为矩阵[x]返回一个指向数组的指针,然后用[y]作为索引。分解一下:
int* row = matrix[ x ];
int val = row[ y ];
方便,是吗?我们喜欢[x][y]语法。
但是这个解决方案有一个很大的缺点,那就是它既胖又慢。
Why?
The reason that it's both fat and slow is actually the same. Each "row" in the matrix is a separately allocated dynamic array. Making a heap allocation is expensive both in time and space. The allocator takes time to make the allocation, sometimes running O(n) algorithms to do it. And the allocator "pads" each of your row arrays with extra bytes for bookkeeping and alignment. That extra space costs...well...extra space. The deallocator will also take extra time when you go to deallocate the matrix, painstakingly free-ing up each individual row allocation. Gets me in a sweat just thinking about it.
它慢还有另一个原因。这些单独的分配往往位于内存的不连续部分。一行的地址可能是1000,另一行的地址可能是100000——你可以理解。这意味着当你在穿越矩阵时,你就像一个狂野的人一样在记忆中跳跃。这往往会导致缓存丢失,从而大大降低处理时间。
所以,如果你绝对必须有你可爱的[x][y]索引语法,使用这个解决方案。如果你想要快速和小巧(如果你不关心这些,为什么要用c++ ?),你需要一个不同的解决方案。
不同的解决方案
更好的解决方案是将整个矩阵分配为单个动态数组,然后使用自己的(稍微)聪明的索引数学来访问单元格。索引的数学运算非常巧妙;不,这一点也不聪明:这是显而易见的。
class Matrix
{
...
size_t index( int x, int y ) const { return x + m_width * y; }
};
给定这个index()函数(我想象它是一个类的成员,因为它需要知道矩阵的m_width),您可以访问矩阵数组中的单元格。矩阵数组是这样分配的:
array = new int[ width * height ];
所以在缓慢的,高脂肪的溶液中
array[ x ][ y ]
...这是一个快速,小的解决方案:
array[ index( x, y )]
很难过,我知道。但你会习惯的。你的CPU会感谢你的。
下面的例子可能会有所帮助,
int main(void)
{
double **a2d = new double*[5];
/* initializing Number of rows, in this case 5 rows) */
for (int i = 0; i < 5; i++)
{
a2d[i] = new double[3]; /* initializing Number of columns, in this case 3 columns */
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
a2d[i][j] = 1; /* Assigning value 1 to all elements */
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
cout << a2d[i][j] << endl; /* Printing all elements to verify all elements have been correctly assigned or not */
}
}
for (int i = 0; i < 5; i++)
delete[] a2d[i];
delete[] a2d;
return 0;
}
我在创建动态数组时使用这个。如果你有一个类或结构。这是可行的。例子:
struct Sprite {
int x;
};
int main () {
int num = 50;
Sprite **spritearray;//a pointer to a pointer to an object from the Sprite class
spritearray = new Sprite *[num];
for (int n = 0; n < num; n++) {
spritearray[n] = new Sprite;
spritearray->x = n * 3;
}
//delete from random position
for (int n = 0; n < num; n++) {
if (spritearray[n]->x < 0) {
delete spritearray[n];
spritearray[n] = NULL;
}
}
//delete the array
for (int n = 0; n < num; n++) {
if (spritearray[n] != NULL){
delete spritearray[n];
spritearray[n] = NULL;
}
}
delete []spritearray;
spritearray = NULL;
return 0;
}