指针到指针动态二维阵列 [英] Pointer-to-pointer dynamic two-dimensional array

查看:239
本文介绍了指针到指针动态二维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本网站上的第一个计时器,所以这里去..

我是一个新手,C ++和我目前经书的工作使用C ++第二版D.S.马利克,数据结构。

在书中马立克提供创建动态二维阵列的方法有两种。
在第一种方法,你声明一个变量是一个指针数组,每个指针为整数类型。恩。

 为int *板[4];

..然后用一个for循环,同时使用指针数组作为行创建列。

第二种方法,您可以使用指针的指针。

  INT **板;
板=新为int * [10];

等。

我的问题是:哪个是更好的方法?该**方法是容易,我可视化,但可在大致相同的方式使用第一种方法。两种方式可用于进行动态的2-D阵列。

编辑:没有与上述职位不够清晰。下面是一些code我尝试:

  INT行,列;COUT<< 输入行大小;
CIN>>行;
COUT<< \\ NCOL:
CIN>>山坳;为int * p_board [行]
的for(int i = 0; I<排;我++)
    p_board [I] =新INT [COL];的for(int i = 0; I<排;我++)
{
    对于(INT J = 0; J<西; J ++)
    {
        p_board [I] [J] = j的;
        COUT<< p_board [I] [J]<< ;
    }
    COUT<< ENDL;
}
COUT<< ENDL<< ENDL;INT ** p_p_board;
p_p_board = INT新* [行]
的for(int i = 0; I<排;我++)
    p_p_board [I] =新INT [COL];的for(int i = 0; I<排;我++)
{
    对于(INT J = 0; J<西; J ++)
    {
        p_p_board [I] [J] = j的;
        COUT<< p_p_board [I] [J]<< ;
    }
    COUT<< ENDL;
}


解决方案

第一种方法不能用来创建的动态二维数组因为这样做的:

 为int *板[4];

您实质上分配的4指向 INT 堆栈阵列。因此,如果你现在填充每个4指针与动态数组:

 的for(int i = 0;我4; ++ I){
  董事会由[i] = INT新[10];
}

您有什么最终得到的是一个二维数组是静态的行数(在这种情况下,4)和动态列数(本例中10)。因此,这不是完全动态的,因为当你在堆栈您分配一个数组应该指定的固定大小,即在编译期知时间即可。 动态数组被称为动态,因为它的大小是没有必要的编译时间可知道,但可以而是由某些变量来确定< STRONG>运行

再一次,当你做:

 为int *板[4];

  const int的X = 4; //&LT; ---`const`预选赛是绝对需要在这种情况下!
为int *板[X];

您提供在编译时已知常数(在这种情况下,4个或 X ),因此编译器现在可以 pre-分配此内存阵列,并且当你的程序被加载到内存中,将已经为阵列这个数量的内存,这是为什么它被称为静态,即由于尺寸的硬codeD 不能更改动态(在运行时)。

在另一方面,当你做:

  INT **板;
板=新为int * [10];

  INT X = 10; //&LT; ---请注意,它并没有被`const`了!
INT **板;
板=新为int * [X];

编译器不知道板数组的内存要求,因此它并不 pre-分配什么。但是当你开始你的程序,数组的大小将由的值确定X 变量(在运行时)和板相应的空间阵列将在所谓的分配 - 内存在您的计算机上运行的所有程序可以分配领域的预先不知道(在编译时)金额内存个人使用。

因此​​,要真正建立动态二维数组,你必须去与第二种方法:

  INT **板;
板=新为int * [10];指针为int的//动态数组(大小为10)对(INT I = 0; I&小于10 ++ⅰ){
  董事会由[i] = INT新[10];
  //每个第i指针现在指向实际整型值动态阵列(尺寸10)
}

我们刚下10个维度创建了10的方形二维数组。要遍历它和实际值填充它,例如1,我们可以使用嵌套循环:

 的for(int i = 0;我小于10; ++ I){//为每行
  为(中间体J = 0; J&小于10; ++ j)条{//为每列
    板[I] [J] = 1;
  }
}

First timer on this website, so here goes..

I'm a newbie to C++ and I'm currently working through the book "Data structures using C++ 2nd ed, of D.S. Malik".

In the book Malik offers two ways of creating a dynamic two-dimensional array. In the first method, you declare a variable to be an array of pointers, where each pointer is of type integer. ex.

int *board[4];

..and then use a for-loop to create the 'columns' while using the array of pointers as 'rows'.

The second method, you use a pointer to a pointer.

int **board;
board = new int* [10]; 

etc.

My question is this: which is the better method? The ** method is easier for me to visualize, but the first method can be used in much the same way. Both ways can be used to make dynamic 2-d arrays.

Edit: Wasn't clear enough with the above post. Here's some code I tried:

int row, col;

cout << "Enter row size:";
cin >> row;
cout << "\ncol:";
cin >> col;

int *p_board[row];
for (int i=0; i < row; i++)
    p_board[i] = new int[col];

for (int i=0; i < row; i++)
{
    for (int j=0; j < col; j++)
    {
        p_board[i][j] = j;
        cout << p_board[i][j] << " ";
    }
    cout << endl;
}
cout << endl << endl;

int **p_p_board;
p_p_board = new int* [row];
for (int i=0; i < row; i++)
    p_p_board[i] = new int[col];

for (int i=0; i < row; i++)
{
    for (int j=0; j < col; j++)
    {
        p_p_board[i][j] = j;
        cout << p_p_board[i][j] << " ";
    }
    cout << endl;
}

解决方案

The first method cannot be used to create dynamic 2D arrays because by doing:

int *board[4];

you essentially allocated an array of 4 pointers to int on stack. Therefore, if you now populate each of these 4 pointers with a dynamic array:

for (int i = 0; i < 4; ++i) {
  board[i] = new int[10];
}

what you end-up with is a 2D array with static number of rows (in this case 4) and dynamic number of columns (in this case 10). So it is not fully dynamic because when you allocate an array on stack you should specify a constant size, i.e. known at compile-time. Dynamic array is called dynamic because its size is not necessary to be known at compile-time, but can rather be determined by some variable in runtime.

Once again, when you do:

int *board[4];

or:

const int x = 4; // <--- `const` qualifier is absolutely needed in this case!
int *board[x];

you supply a constant known at compile-time (in this case 4 or x) so that compiler can now pre-allocate this memory for your array, and when your program is loaded into the memory it would already have this amount of memory for the board array, that's why it is called static, i.e. because the size is hard-coded and cannot be changed dynamically (in runtime).

On the other hand, when you do:

int **board;
board = new int*[10];

or:

int x = 10; // <--- Notice that it does not have to be `const` anymore!
int **board;
board = new int*[x];

the compiler does not know how much memory board array will require, and therefore it does not pre-allocate anything. But when you start your program, the size of array would be determined by the value of x variable (in runtime) and the corresponding space for board array would be allocated on so-called heap - the area of memory where all programs running on your computer can allocate unknown beforehand (at compile-time) amounts memory for personal usage.

As a result, to truly create dynamic 2D array you have to go with the second method:

int **board;
board = new int*[10]; // dynamic array (size 10) of pointers to int

for (int i = 0; i < 10; ++i) {
  board[i] = new int[10];
  // each i-th pointer is now pointing to dynamic array (size 10) of actual int values
}

We've just created an square 2D array with 10 by 10 dimensions. To traverse it and populate it with actual values, for example 1, we could use nested loops:

for (int i = 0; i < 10; ++i) {   // for each row
  for (int j = 0; j < 10; ++j) { // for each column
    board[i][j] = 1;
  }
}

这篇关于指针到指针动态二维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆