我如何声明一个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]
所做的事。
当前回答
在c++ 11中可以:
auto array = new double[M][N];
这样,内存就不会被初始化。要初始化它,可以这样做:
auto array = new double[M][N]();
示例程序(用"g++ -std=c++11"编译):
#include <iostream>
#include <utility>
#include <type_traits>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
int main()
{
const auto M = 2;
const auto N = 2;
// allocate (no initializatoin)
auto array = new double[M][N];
// pollute the memory
array[0][0] = 2;
array[1][0] = 3;
array[0][1] = 4;
array[1][1] = 5;
// re-allocate, probably will fetch the same memory block (not portable)
delete[] array;
array = new double[M][N];
// show that memory is not initialized
for(int r = 0; r < M; r++)
{
for(int c = 0; c < N; c++)
cout << array[r][c] << " ";
cout << endl;
}
cout << endl;
delete[] array;
// the proper way to zero-initialize the array
array = new double[M][N]();
// show the memory is initialized
for(int r = 0; r < M; r++)
{
for(int c = 0; c < N; c++)
cout << array[r][c] << " ";
cout << endl;
}
int info;
cout << abi::__cxa_demangle(typeid(array).name(),0,0,&info) << endl;
return 0;
}
输出:
2 4
3 5
0 0
0 0
double (*) [2]
其他回答
如果你想声明一个预定义的指针数组:
int **x;
x = new int*[2] {
new int[2] { 0, 1 },
new int[2] { 2, 3 }
};
访问:
cout << x[0][0];
在c++ 11中可以:
auto array = new double[M][N];
这样,内存就不会被初始化。要初始化它,可以这样做:
auto array = new double[M][N]();
示例程序(用"g++ -std=c++11"编译):
#include <iostream>
#include <utility>
#include <type_traits>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
int main()
{
const auto M = 2;
const auto N = 2;
// allocate (no initializatoin)
auto array = new double[M][N];
// pollute the memory
array[0][0] = 2;
array[1][0] = 3;
array[0][1] = 4;
array[1][1] = 5;
// re-allocate, probably will fetch the same memory block (not portable)
delete[] array;
array = new double[M][N];
// show that memory is not initialized
for(int r = 0; r < M; r++)
{
for(int c = 0; c < N; c++)
cout << array[r][c] << " ";
cout << endl;
}
cout << endl;
delete[] array;
// the proper way to zero-initialize the array
array = new double[M][N]();
// show the memory is initialized
for(int r = 0; r < M; r++)
{
for(int c = 0; c < N; c++)
cout << array[r][c] << " ";
cout << endl;
}
int info;
cout << abi::__cxa_demangle(typeid(array).name(),0,0,&info) << endl;
return 0;
}
输出:
2 4
3 5
0 0
0 0
double (*) [2]
如果行长是编译时常数,c++ 11允许
auto arr2d = new int [nrows][CONSTANT];
请看这个答案。像gcc这样的编译器允许将变长数组作为c++的扩展,可以使用如下所示的new来获得完全的运行时可变数组维度功能,就像C99所允许的那样,但是可移植的ISO c++仅限于第一个维度是变量。
另一个有效的选择是手动对一个大的1d数组进行2d索引,正如另一个答案所示,允许与真正的2d数组相同的编译器优化(例如,证明或检查数组不会彼此别名/重叠)。
否则,您可以使用指向数组的指针数组来支持类似连续2D数组的2D语法,尽管这不是一个有效的单一大分配。你可以使用循环初始化它,就像这样:
int** a = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
a[i] = new int[colCount];
上面,对于colCount= 5和rowCount = 4,将产生以下结果:
在删除指针数组之前,不要忘记使用循环单独删除每一行。另一个答案中的例子。
这个答案的目的不是添加其他答案没有涵盖的新内容,而是扩展@Kevin Loney的答案。
你可以使用轻量级声明:
int *ary = new int[SizeX*SizeY]
访问语法将是:
ary[i*SizeY+j] // ary[i][j]
但这对大多数人来说都很麻烦,可能会导致混乱。所以,你可以这样定义宏:
#define ary(i, j) ary[(i)*SizeY + (j)]
现在可以使用非常相似的语法ary(i, j) //表示ary[i][j]。 这具有简单美观的优点,同时,使用表达式代替索引也更简单,不那么令人困惑。
要访问,比如说,ary[2+5][3+8],你可以写ary(2+ 5,3 +8),而不是看起来复杂的ary[(2+5)*SizeY +(3+8)],也就是说,它节省了括号,有助于可读性。
警告:
尽管语法非常相似,但并不相同。 如果将数组传递给其他函数,则必须以相同的名称传递SizeY(或者声明为全局变量)。
或者,如果你需要在多个函数中使用数组,那么你可以在宏定义中添加SizeY作为另一个参数,如下所示:
#define ary(i, j, SizeY) ary[(i)*(SizeY)+(j)]
你懂的。当然,这会变得太长而没有用处,但它仍然可以防止+和*的混淆。
当然不推荐这样做,大多数有经验的用户会谴责这是一种糟糕的做法,但我还是忍不住要分享它,因为它很优雅。
编辑: 如果你想要一个适用于任意数量数组的可移植解决方案,你可以使用以下语法:
#define access(ar, i, j, SizeY) ar[(i)*(SizeY)+(j)]
然后你可以使用访问语法将任意大小的数组传递给调用:
access(ary, i, j, SizeY) // ary[i][j]
附注:我已经测试了这些,在g++14和g++11编译器上可以使用相同的语法(作为左值和右值)。
如果只在本地或类中使用数组,则可以使用lambda表达式创建类数组对象。
int main(int argc, char **argv) {
srand(time(0));
int w = rand() % 20 + 10;
int h = rand() % 20 + 10;
{
// Here. Only one line and no memory leak.
auto arr = [=, p = std::make_unique<int[]>(w * h)](int i, int j) -> int & { return *&p[i * w + j]; };
arr(1, 0) = 6;
std::cout << arr(1, 0) << '\n';
}
}
该对象与int[][]不兼容,要将其作为参数传递,需要模板。但是当你访问一个元素时,它可以让你摆脱正确获取[i * w + j]的麻烦。
你也可以使用一个裸std::unique_ptr<int[]>和一个在lambda中实现的getter。但你可能会犯错误,引用不明确。