C++ 指向c+中的指针+;

C++ 指向c+中的指针+;,c++,pointers,C++,Pointers,这段代码无论我怎么努力都无法理解 #include <iostream> using namespace std; int main() { int ***mat; mat = new int**[4]; for(int h = 0; h < 4; h++) { mat[h] = new int*[4]; } for (int i = 0; i < 4; i++) { delete[] ma

这段代码无论我怎么努力都无法理解

#include <iostream>

using namespace std;

int main()
{
    int ***mat;

    mat = new int**[4];

    for(int h = 0; h < 4; h++) {
        mat[h] = new int*[4];
    }

    for (int i = 0; i < 4; i++) {
        delete[] mat[i];
        delete[] mat;
    }

    return 0;
}
#包括
使用名称空间std;
int main()
{
int***mat;
mat=新整数**[4];
对于(int h=0;h<4;h++){
mat[h]=新整数*[4];
}
对于(int i=0;i<4;i++){
删除[]mat[i];
删除[]项;
}
返回0;
}
这不是应该
mat=newint**[4]
意味着mat将指向一个
int**
数组,所以当我想使用这个数组的成员时,我应该执行
*mat[0]


我不明白这行
mat[h]=newint*[4]

这里有一个关于内文注释和更正代码的逐步解释

#include <iostream>
using namespace std;
int main()
{
    int ***mat; // placeholder for a 3D matrix, three dimensional storage of integers
    // say for 3d matrix, you have height(z-dimension), row and columns
    mat = new int**[4]; // allocate for one dimension, say height
    for(int h = 0; h < 4; h++) {
        mat[h] = new int*[4]; // allocate 2nd dimension, say for rows per height
    }
    // now you should allocate space for columns (3rd dimension)
    for(int h = 0; h < 4; h++) {
       for (int r = 0; r < 4; r++) {
          mat[h][r] = new int[4]; // allocate 3rd dimension, say for cols per row
    }}
    // now you have the matrix ready as 4 x 4 x 4 
    // for deallocation, delete column first, then row, then height
    // rule is deallocate in reverse order of allocation
    for(int h = 0; h < 4; h++) {
       for (int r = 0; r < 4; r++) {
          delete [] mat[h][r]; // deallocate 3rd dimension, say for cols per row
       }
       delete [] mat[h]; // deallocate 2nd dimension, rows per height
    }
    delete [] mat; // deallocate height, i.e. entire matrix
    return 0;
}
#包括
使用名称空间std;
int main()
{
int***mat;//三维矩阵的占位符,整数的三维存储
//例如,对于3d矩阵,有高度(z尺寸)、行和列
mat=new int**[4];//分配一个维度,比如高度
对于(int h=0;h<4;h++){
mat[h]=new int*[4];//分配第二维度,例如每高度的行数
}
//现在您应该为列分配空间(三维)
对于(int h=0;h<4;h++){
对于(int r=0;r<4;r++){
mat[h][r]=new int[4];//分配第三维空间,例如每行的列数
}}
//现在矩阵准备好了,为4x4x4
//要取消分配,请先删除列,然后删除行,然后删除高度
//规则按与分配相反的顺序解除分配
对于(int h=0;h<4;h++){
对于(int r=0;r<4;r++){
删除[]mat[h][r];//取消分配第三维空间,例如每行的列数
}
删除[]mat[h];//取消分配第二个维度,每个高度的行数
}
删除[]矩阵;//取消分配高度,即整个矩阵
返回0;
}

这里有一个逐步的解释,包括注释和更正的代码

#include <iostream>
using namespace std;
int main()
{
    int ***mat; // placeholder for a 3D matrix, three dimensional storage of integers
    // say for 3d matrix, you have height(z-dimension), row and columns
    mat = new int**[4]; // allocate for one dimension, say height
    for(int h = 0; h < 4; h++) {
        mat[h] = new int*[4]; // allocate 2nd dimension, say for rows per height
    }
    // now you should allocate space for columns (3rd dimension)
    for(int h = 0; h < 4; h++) {
       for (int r = 0; r < 4; r++) {
          mat[h][r] = new int[4]; // allocate 3rd dimension, say for cols per row
    }}
    // now you have the matrix ready as 4 x 4 x 4 
    // for deallocation, delete column first, then row, then height
    // rule is deallocate in reverse order of allocation
    for(int h = 0; h < 4; h++) {
       for (int r = 0; r < 4; r++) {
          delete [] mat[h][r]; // deallocate 3rd dimension, say for cols per row
       }
       delete [] mat[h]; // deallocate 2nd dimension, rows per height
    }
    delete [] mat; // deallocate height, i.e. entire matrix
    return 0;
}
#包括
使用名称空间std;
int main()
{
int***mat;//三维矩阵的占位符,整数的三维存储
//例如,对于3d矩阵,有高度(z尺寸)、行和列
mat=new int**[4];//分配一个维度,比如高度
对于(int h=0;h<4;h++){
mat[h]=new int*[4];//分配第二维度,例如每高度的行数
}
//现在您应该为列分配空间(三维)
对于(int h=0;h<4;h++){
对于(int r=0;r<4;r++){
mat[h][r]=new int[4];//分配第三维空间,例如每行的列数
}}
//现在矩阵准备好了,为4x4x4
//要取消分配,请先删除列,然后删除行,然后删除高度
//规则按与分配相反的顺序解除分配
对于(int h=0;h<4;h++){
对于(int r=0;r<4;r++){
删除[]mat[h][r];//取消分配第三维空间,例如每行的列数
}
删除[]mat[h];//取消分配第二个维度,每个高度的行数
}
删除[]矩阵;//取消分配高度,即整个矩阵
返回0;
}

一个级别的“指针”是隐含的

new int;
将给您一个
int*
,您也将这样做

new int[3];

从那里向上移动

一个层次的“指针”是隐含的

new int;
将给您一个
int*
,您也将这样做

new int[3];

从那里向上移动

mat[0]
是该数组的成员
mat[0][0]
是由
mat[0]
指向的数组的成员。(如果您不知道,
*X
是处理数组或指针时编写
X[0]
的另一种方式)
mat[h]
也是该数组的成员。你没有“得到”的那一行是让这个成员指向新分配的数组的第一个元素。如果你对自己说“指向数组的第一个元素”而不是“指向数组”,那么可能就不会那么令人困惑了。你在调用指针“数组”时必须小心,因为指针可以指向数组(或者更确切地说,它的第一个元素),指向指针的指针与数组数组不同。指针和数组彼此不同。(例如
&array==&array[0]==array
,忽略数据类型。但是
&ptr!=&ptr[0]
)。但在许多情况下,编译器会相互转换语法。
*(a+i)a[i]
。所以要小心,尤其是在使用多维数组时。好吧,我从来都不知道你能做int*cat;cout
mat[0]
是该数组的一员。
mat[0][0]
是由
mat[0]
指向的数组的一员(如果你不知道,
*X
是另一种写入
X[0]的方式)
,当我们处理数组或指针时)。
mat[h]
也是该数组的一个成员。您没有“得到”的那一行使该成员指向新分配的数组的第一个元素。如果您对自己说“指向数组的第一个元素”而不是“指向数组”这可能不那么令人困惑。在调用指针“数组”时,您必须小心,因为虽然指针可以指向数组(或者更确切地说,它的第一个元素),但指向指针的指针与数组的指针不同。指针和数组彼此不同。(例如
&array==&array[0]==array
,忽略数据类型。但是
&ptr!=&ptr[0]
)。但是在很多情况下,编译器会相互转换语法。
*(a+i)a[i]
。所以要小心,尤其是在使用多维数组时。好吧,我从来都不知道你能做int*cat;cout