How to dynamically establish a two-dimensional array (go) in C + +

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_7c073a8d0100qp1w.html

http://blog.163.com/[email protected]/blog/static/7198839920117252550574/

The format of creating two-dimensional arrays dynamically with new in C + + is generally the case:

Type (*P) [N] = new TYPE [][n];

Where type is a kind, n is the number of columns in a two-dimensional array. In this format, the number of columns must be indicated, while the number of rows does not need to be specified. Here, the type of P is type*[n], which is a pointer to an array of N-column elements.

There is also a way to not specify the number of columns in an array:

int **p;
p = new INT*[10]; Note that int*[10] represents an array of pointers with 10 elements
for (int i = 0; I! = ten; ++i)
{
P[i] = new INT[5];
}

Here is a pointer to the pointer, which points to a pointer array containing 10 elements, and each element points to an array of 5 elements, thus constructing an array of 10 rows and 5 columns.


When the array is used, the code that frees the space is:

for (int i = 0; I! = 5; i++)
{
Delete[] p[i];
}
Delete[] p;

To deal with two-dimensional arrays, you can use a reduced-dimension or two-dimensional method.
The dimensionality reduction method uses an array to accept the two-dimensional array, the first address of the two-dimensional element &a[0][0] as a parameter, and the function is accepted by int *.
The two-dimensional method is directly accepted by a two-dimensional array, but the number of columns needs to be specified.

Double **data;data = new Double*[m]; Set line or direct double **data=new double*[m]; A pointer to an array of pointers. for (int j=0;j<m;j++) {Data[j] = new Double[n];        Each pointer element of this pointer array points to an array. }for (int i=0;i<m;i++) {for   (int j=0;j<n;j++)   {    data[i][j]=i*n+j;//initialize array element   }}for (i=0;i<m; i++) {delete[] data[i];//undo the array that the pointer element points to}                     delete[] data;  /* This method is done by dynamically creating an array of pointers and then dynamically pointing to an array for each element of the pointer array. The process of creation is two important to the destruction process. During the destruction process, the array of pointers to each element of the pointer array is destroyed before the array of pointers is destroyed. */

  

How to dynamically establish a two-dimensional array (go) in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.