我如何声明一个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]
其他回答
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;
动态声明2D数组:
#include<iostream>
using namespace std;
int main()
{
int x = 3, y = 3;
int **ptr = new int *[x];
for(int i = 0; i<y; i++)
{
ptr[i] = new int[y];
}
srand(time(0));
for(int j = 0; j<x; j++)
{
for(int k = 0; k<y; k++)
{
int a = rand()%10;
ptr[j][k] = a;
cout<<ptr[j][k]<<" ";
}
cout<<endl;
}
}
现在,在上面的代码中,我们获取了一个双指针,并为它分配了一个动态内存,并给出了列的值。这里分配的内存仅用于列,现在对于行,我们只需要一个for循环,并为每一行分配一个动态内存。现在我们可以像使用2D数组一样使用指针。在上面的例子中,我们将随机数分配给我们的2D数组(指针)。这都是关于二维数组的DMA。
我不确定是否没有提供以下答案 但我决定在2d数组的分配中添加一些局部优化 (例如,一个方阵只通过一次分配完成): Int ** mat = new Int *[n]; Mat [0] = new int [n * n];
然而,由于上述分配的线性,删除是这样的: 删除[]mat[0]; 删除[]mat;
首先使用指针定义数组(第1行):
int** a = new int* [x]; //x is the number of rows
for(int i = 0; i < x; i++)
a[i] = new int[y]; //y is the number of columns
这里,我有两个选择。第一个展示了数组的数组或指针的指针的概念。我更喜欢第二个,因为地址是连续的,正如您在图像中看到的那样。
#include <iostream>
using namespace std;
int main(){
int **arr_01,**arr_02,i,j,rows=4,cols=5;
//Implementation 1
arr_01=new int*[rows];
for(int i=0;i<rows;i++)
arr_01[i]=new int[cols];
for(i=0;i<rows;i++){
for(j=0;j<cols;j++)
cout << arr_01[i]+j << " " ;
cout << endl;
}
for(int i=0;i<rows;i++)
delete[] arr_01[i];
delete[] arr_01;
cout << endl;
//Implementation 2
arr_02=new int*[rows];
arr_02[0]=new int[rows*cols];
for(int i=1;i<rows;i++)
arr_02[i]=arr_02[0]+cols*i;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++)
cout << arr_02[i]+j << " " ;
cout << endl;
}
delete[] arr_02[0];
delete[] arr_02;
return 0;
}