我如何声明一个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++中这样定义动态数组
int sizeY,sizeX = 10;
//declaring dynamic 2d array:
int **ary = new int*[sizeY];
for (int i = 0; i < sizeY; i++)
{
ary[i] = new int[sizeX];
}
您可以像这样在运行时更改大小。 这是在c++ 98中测试的
其他回答
在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]
我在创建动态数组时使用这个。如果你有一个类或结构。这是可行的。例子:
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;
}
为什么不使用STL:vector?很简单,你不需要删除向量。
int rows = 100;
int cols = 200;
vector< vector<int> > f(rows, vector<int>(cols));
f[rows - 1][cols - 1] = 0; // use it like arrays
你也可以初始化“数组”,只是给它一个默认值
const int DEFAULT = 1234;
vector< vector<int> > f(rows, vector<int>(cols, DEFAULT));
来源:如何在C/ c++中创建2,3(或多)维数组?
如果你的项目是CLI(公共语言运行时支持),那么:
你可以使用数组类,而不是你写的那个:
#include <array>
using namespace std;
换句话说,不是使用std名称空间和包含数组头时获得的非托管数组类,不是std名称空间和数组头中定义的非托管数组类,而是CLI的托管类数组。
使用这个类,您可以创建任意级别的数组。
下面的代码创建了一个2行3列的二维数组,类型为int,并命名为“arr”:
array<int, 2>^ arr = gcnew array<int, 2>(2, 3);
现在您可以访问数组中的元素,通过命名它并只写一个平方括号[],并在其中添加行和列,并用逗号分隔它们。
下面的代码访问我已经在上面的代码中创建的数组的第2行和第1列中的一个元素:
arr[0, 1]
只写这一行是为了读取该单元格中的值,即获取该单元格中的值,但如果添加等号,则要写入该单元格中的值,即设置该单元格中的值。 当然,你也可以使用+=,-=,*=和/=运算符,仅用于数字(int, float, double, __int16, __int32, __int64等),但你肯定已经知道了。
如果您的项目不是CLI,那么您可以使用std名称空间的非托管数组类,当然,如果您使用#include <array>,但是问题是这个数组类与CLI数组不同。创建这种类型的数组与CLI相同,除了必须删除^符号和gcnew关键字。但不幸的是,<>括号中的第二个int参数指定了数组的长度(即大小),而不是它的秩!
没有办法在这类数组中指定rank, rank是CLI数组的特性。
STD数组的行为类似于c++中的普通数组,使用指针定义,例如int*,然后:new int[size],或者不使用指针:int arr[size],但与c++中的普通数组不同,STD数组提供了可以与数组元素一起使用的函数,如fill, begin, end, size等,但普通数组什么也没有提供。
但是std数组仍然是一维数组,就像普通的c++数组一样。 但是多亏了其他人提出的关于如何将普通的c++一维数组转换为二维数组的解决方案,我们可以将同样的想法应用到std数组中,例如,根据Mehrdad Afshari的想法,我们可以编写以下代码:
array<array<int, 3>, 2> array2d = array<array<int, 3>, 2>();
这一行代码创建了一个“罐子数组”,这是一个一维数组,它的每个单元格都是或指向另一个一维数组。
如果一维数组中所有一维数组的长度/大小都相等,那么你可以将array2d变量视为一个真正的二维数组,另外你可以使用特殊的方法来处理行或列,这取决于你如何看待它,在二维数组中,std数组支持。
你也可以使用Kevin Loney的解决方案:
int *ary = new int[sizeX*sizeY];
// ary[i][j] is then rewritten as
ary[i*sizeY+j]
但如果你使用STD数组,代码必须看起来不同:
array<int, sizeX*sizeY> ary = array<int, sizeX*sizeY>();
ary.at(i*sizeY+j);
并且仍然具有std数组的独特功能。
请注意,您仍然可以使用[]括号访问std数组的元素,并且不必调用at函数。 您还可以定义并赋值一个新的int变量,该变量将计算并保持std数组中元素的总数,并使用它的值,而不是重复sizeX* sizeey
You can define your own two dimensional array generic class, and define the constructor of the two dimensional array class to receive two integers to specify the number of rows and columns in the new two dimensional array, and define get function that receive two parameters of integer that access an element in the two dimensional array and returns its value, and set function that receives three parameters, that the two first are integers that specify the row and column in the two dimensional array, and the third parameter is the new value of the element. Its type depends on the type you chose in the generic class.
你将能够通过使用普通的c++数组(指针或不指针)或std数组来实现所有这些,并使用其他人建议的想法之一,并使其易于使用,如cli数组,或像你可以在c#中定义、赋值和使用的二维数组。
2D数组基本上是一个指针的1D数组,其中每个指针都指向一个1D数组,该数组将保存实际数据。
这里N是行,M是列。
动态分配
int** ary = new int*[N];
for(int i = 0; i < N; i++)
ary[i] = new int[M];
fill
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
ary[i][j] = i;
打印
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
std::cout << ary[i][j] << "\n";
free
for(int i = 0; i < N; i++)
delete [] ary[i];
delete [] ary;