Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 没有';无法理解使用双指针(**)创建二维动态数组_C++_Arrays - Fatal编程技术网

C++ 没有';无法理解使用双指针(**)创建二维动态数组

C++ 没有';无法理解使用双指针(**)创建二维动态数组,c++,arrays,C++,Arrays,我在理解双指针方面有些问题。好吧,我知道,它的意思是“指针到指针”。但当我在代码中看到它时,我就是不明白。例如: // Create Dynamic 2D-Array int **arr = new int*[rows]; for (int i = 0; i < rows; i++) { arr[i] = new int[cols]; } //创建动态二维阵列 int**arr=新int*[行]; 对于(int i=0;

我在理解双指针方面有些问题。好吧,我知道,它的意思是“指针到指针”。但当我在代码中看到它时,我就是不明白。例如:


// Create  Dynamic 2D-Array
    int **arr = new int*[rows];

    for (int i = 0; i < rows; i++)
    {
        arr[i] = new int[cols];     
    }


//创建动态二维阵列
int**arr=新int*[行];
对于(int i=0;i
这是我的老师用来创建2d动态数组的代码,但我不懂。有人能解释一下吗?例如,what
int**arr=newint*[rows]意味着什么?或者我们在那里用了什么?

还有一个问题:有没有办法不使用指针创建2d动态数组?

一些观察结果可能会帮助您:

  • 指针占用内存的方式与任何其他变量相同。它类似于任何其他类型。因此,如果为变量
    i
    绘制一个小矩形,也可以为指针绘制一个

  • 使用
    typedef
    使用
    创建别名并简化复杂类型的定义:

    using pointer_to_int = int*;
    
    pointer_to_int* arr = new pointer_to_int[rows]; // allocates an array of pointer_to_int (similar to new int[rows])
    
    for (int i = 0; i < rows; i++)
    {
      arr[i] = new int[cols]; // arr[i] is of type pointer_to_int
    }
    
    使用指向int=int*的指针;
    指针指向_int*arr=新指针指向_int[行];//将指针数组\u分配给\u int(类似于新的int[行])
    对于(int i=0;i

  • “有没有办法不使用指针就创建2d动态数组?”这取决于您所查看的级别。最后,在某个地方,会有一个指针。您可以使用1d数组(
    向量
    ,如果允许)并将其除以行(或列)。如果行有3列,第一行从索引
    0
    开始,第二行从索引
    3
    开始,第三行从索引
    6
    开始,等等。请参阅。

    它创建一个指针数组,该数组中的每个指针指向内存中的不同位置

    +-arr--+
    |      |     +---------------+
    |   0  |  -> | ... cols      |
    |      |     +---------------+
    +------+
    |      |     +---------------+
    |   1  |  -> | ... cols      |
    |      |     +---------------+
    +------+
    ...
    
    +------+
    |      |     +---------------+
    |rows-1|  -> | ... cols      |
    |      |     +---------------+
    +------+
    
    因此,它不像您将其声明为

    int arr[rows][cols];
    

    虽然您将在90年代到2011年(以及过去的一些)编写的代码中看到许多此类代码。这种类型的指针集合允许使用2D数组索引访问元素,它已被
    std::vector
    (在本例中为向量向量)所取代

    首先要了解的是
    int**arr
    声明指向指向pointer-to-int的单指针(也称为双指针)。不管怎样,你都有一个指向什么的指针?(指向类型的指针,无论使用的是什么
    类型

    为了使这种方法有用,首先要分配所需的指针数量。下面分配一个内存块,其中包含指向int的
    行数
    指针,并将第一个指针的地址分配给
    arr

    int **arr = new int*[rows];
    
    现在您有了
    个未初始化指针。(例如,您有一块能够容纳
    指针的内存)。要访问
    arr
    中的每个指针,只需使用数组索引(例如
    arr[2]
    是分配的块中的第三个指针)。等效指针表示法为
    *(arr+2)
    。(注意:在指针表示法中,
    […]
    作为解引用的作用与
    *(…)
    作为解引用的作用相同)

    为了有用,分配的块中的每个指针必须指向包含一定数量整数的有效内存。您可以为现有整数数组分配地址,例如

    #define COLS 5
    ...
        int myarray[COLS] = {0};       /* array of COLS integer values initialized zero */
        int **arr = new int*[rows];    /* your allocation a block for rows pointers to int */
        ...
        arr[0] = myarray;              /* assigning pointer to existing block to arr[0] */
    
    在您的情况下,您只需分配一个内存块,该内存块可以包含整数的
    cols
    个数,并将该块的起始地址分配给分配给
    int**arr=new int*[rows]的块中的一个指针。以下代码就是这样做的:

    // Create rows pointers pointing to allocated blocks of cols integers
    int **arr = new int*[rows];        /* allocated block of rows pointers */
    
    for (int i = 0; i < rows; i++)     /* for each pointer */
    {
        arr[i] = new int[cols];        /* allocate & assign block of cols int */
    }
    
    //创建指向已分配cols整数块的行指针
    int**arr=新int*[行];/*已分配的行指针块*/
    对于(int i=0;i
    现在您的对象已完成。您有一个大小为的内存块,因此它可以包含
    个指针,您已经分配了
    可容纳
    cols的内存块数
    整数数,并且已经将容纳
    cols的每个内存块的起始地址
    整数数分配给原始分配块中包含的每个指针


    由于您可以使用
    arr[x]
    where
    0访问每个指针,因此实际上并不创建2D数组;它创建一个指针、一个指针数组和一堆数组。FWIW,不要使用双指针。如果您想要矩阵或更高阶多维“数组”,请使用单个维度
    std::vector
    作为类的成员,并使用数学假装它有多个维度。