C++ c++;二维数组和指向指针的指针-我不&x27;我不懂这个代码

C++ c++;二维数组和指向指针的指针-我不&x27;我不懂这个代码,c++,C++,我不理解指向指针的指针或指向2d数组的指针。我不明白下面的代码是做什么的。有人能逐行向我解释一下它在做什么吗?对我来说,理解这个概念非常重要,但我无法理解 #include <iostream> #include <iomanip> using namespace std; int main() { //i understand that we declare a 2d array int tD[2][2]; //but then i'm co

我不理解指向指针的指针或指向2d数组的指针。我不明白下面的代码是做什么的。有人能逐行向我解释一下它在做什么吗?对我来说,理解这个概念非常重要,但我无法理解

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    //i understand that we declare a 2d array
    int tD[2][2];
    //but then i'm confused why there is a pointer to a pointer when there isn't a pointer in the first place
    int **tD2;
    //and i am confused what the star after int does
    tD2 = new int*[2];

    //i think i get this
    for(int i = 0; i < 2; i++)
        tD2[i] = new int[2];

    for(int i = 0; i < 2; i++)
        delete [] tD2[i];

    //lost here
    delete [] tD2;

    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
//我知道我们声明了一个2d数组
国际运输署[2][2];;
//但是我很困惑为什么一开始就没有指针,却有指向指针的指针
int**tD2;
//我不知道int后面的星是干什么的
tD2=新整数*[2];
//我想我明白了
对于(int i=0;i<2;i++)
tD2[i]=新的整数[2];
对于(int i=0;i<2;i++)
删除[]tD2[i];
//迷失在这里
删除[]tD2;
返回0;
}

我会留下评论来解释

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    //i understand that we declare a 2d array
    int tD[2][2];

    //but then i'm confused why there is a pointer to a pointer when there isn't a pointer in the first place
    int **tD2; // A 1D array is an int*; int** makes an array of int*'s, which are themselves arrays (not necessarily all next to each other).

    //and i am confused what the star after int does
    tD2 = new int*[2]; // allocates memory for two int*'s.
    /*
        int* a = new int[2];
        int** b = new int*[2];
        int*** c = new int**[2];

        See the pattern? It's one less * than the type.
    */

    //i think i get this
    for(int i = 0; i < 2; i++)
        tD2[i] = new int[2];

    for(int i = 0; i < 2; i++)
        delete [] tD2[i]; // deallocates each inner array.

    //lost here
    delete [] tD2; // deallocate the outer array. (i.e., the array that holds the "inner arrays").

    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
//我知道我们声明了一个2d数组
国际运输署[2][2];;
//但是我很困惑为什么一开始就没有指针,却有指向指针的指针
int**tD2;//1D数组是int*;int**生成int*的数组,它们本身就是数组(不一定都是相邻的)。
//我不知道int后面的星是干什么的
tD2=new int*[2];//为两个int*分配内存。
/*
int*a=新的int[2];
int**b=新的int*[2];
int***c=新int**[2];
看到图案了吗?比样式少一个。
*/
//我想我明白了
对于(int i=0;i<2;i++)
tD2[i]=新的整数[2];
对于(int i=0;i<2;i++)
delete[]tD2[i];//取消分配每个内部数组。
//迷失在这里
delete[]tD2;//取消分配外部数组(即,保存“内部数组”的数组)。
返回0;
}

我会留下评论来解释

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    //i understand that we declare a 2d array
    int tD[2][2];

    //but then i'm confused why there is a pointer to a pointer when there isn't a pointer in the first place
    int **tD2; // A 1D array is an int*; int** makes an array of int*'s, which are themselves arrays (not necessarily all next to each other).

    //and i am confused what the star after int does
    tD2 = new int*[2]; // allocates memory for two int*'s.
    /*
        int* a = new int[2];
        int** b = new int*[2];
        int*** c = new int**[2];

        See the pattern? It's one less * than the type.
    */

    //i think i get this
    for(int i = 0; i < 2; i++)
        tD2[i] = new int[2];

    for(int i = 0; i < 2; i++)
        delete [] tD2[i]; // deallocates each inner array.

    //lost here
    delete [] tD2; // deallocate the outer array. (i.e., the array that holds the "inner arrays").

    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
//我知道我们声明了一个2d数组
国际运输署[2][2];;
//但是我很困惑为什么一开始就没有指针,却有指向指针的指针
int**tD2;//1D数组是int*;int**生成int*的数组,它们本身就是数组(不一定都是相邻的)。
//我不知道int后面的星是干什么的
tD2=new int*[2];//为两个int*分配内存。
/*
int*a=新的int[2];
int**b=新的int*[2];
int***c=新int**[2];
看到图案了吗?比样式少一个。
*/
//我想我明白了
对于(int i=0;i<2;i++)
tD2[i]=新的整数[2];
对于(int i=0;i<2;i++)
delete[]tD2[i];//取消分配每个内部数组。
//迷失在这里
delete[]tD2;//取消分配外部数组(即,保存“内部数组”的数组)。
返回0;
}
这将动态地将两个指针数组(
*
)分配给
int
s,并将此数组分配给
tD2
<代码>tD2现在指向某个东西,可以安全使用。但是,指针数组未初始化且危险

    //and i am confused what the star after int does
    tD2 = new int*[2];
    //i think i get this
    for(int i = 0; i < 2; i++)
        tD2[i] = new int[2];
您动态分配的任何内存都应该在使用完后返回到它所在的位置,以便可以重用内存,否则最终您的程序将耗尽内存
delete[]
返回一个数组,并确保调用适当的析构函数。此循环释放
int
的数组

    //lost here
    delete [] tD2;
出于与上述相同的原因,释放指向
int
s的指针数组

    return 0;
}
注意:这是一种可怕的管理2D阵列的方法。看看你要做的所有工作。想想忘记或无法返回分配的内存是多么容易。程序员必须记住尺寸或用
tD2
传递尺寸,以确保没有人越界

不要这样做

#包括
#包括
#包括
使用名称空间std;
int main(){
//我知道我们声明了一个2d数组
国际运输署[2][2];;
向量tD2(2,//外向量包含2个向量
向量(2));//内部向量包含2个整数
返回0;
}
vector
为您照看内存。它知道所有的维度有多大,所以很难迷路。它有一个方法,
at
,它不会让你越界。如果你需要它变大,它就会变大。拿那个动态数组!最棒的是,它有很多支持搜索、排序和操作功能的库。你必须是一个傻瓜才能不使用向量

别傻了

如果你的老师强迫你做傻子,你可以私下里叫他们傻子,假装你是傻子,直到你安全地通过了这门课

这将动态地将两个指针数组(
*
)分配给
int
s,并将此数组分配给
tD2
<代码>tD2现在指向某个东西,可以安全使用。但是,指针数组未初始化且危险

    //and i am confused what the star after int does
    tD2 = new int*[2];
    //i think i get this
    for(int i = 0; i < 2; i++)
        tD2[i] = new int[2];
您动态分配的任何内存都应该在使用完后返回到它所在的位置,以便可以重用内存,否则最终您的程序将耗尽内存
delete[]
返回一个数组,并确保调用适当的析构函数。此循环释放
int
的数组

    //lost here
    delete [] tD2;
出于与上述相同的原因,释放指向
int
s的指针数组

    return 0;
}
注意:这是一种可怕的管理2D阵列的方法。看看你要做的所有工作。想想忘记或无法返回分配的内存是多么容易。程序员必须记住尺寸或用
tD2
传递尺寸,以确保没有人越界

不要这样做

#包括
#包括
#包括
使用名称空间std;
int main(){
//我知道我们声明了一个2d数组
国际运输署[2][2];;
向量tD2(2,//o