Two-dimensional arrays

Source: Internet
Author: User

Two-dimensional array definitions:

General form: type specifier array name [constant expression 1][constant expression 2]

int a[2][3];

 

Using the C + + STL vector definition:

vector< vector<int> > Array (2, vector<int> (0)); Define the number of rows as 2

for (int i=0; i<2; i++)

Array[i].resize (3); Defines the number of columns as 3

    

Initialization of two-dimensional arrays:

Fragment assignment: int a[2[3]={{1, 2, 3},{4, 5, 6}}//row of constant expression can be omitted;

Continuous assignment: int a[2[3]={1, 2, 3, 4}//partial element assignment, followed by automatic assignment of 0

Parameter passing for two-dimensional arrays:

void Fun (int a[][3]) {...} Note that array a passes the parameter and then degenerates to a pointer

Dynamic allocation and release of two-dimensional arrays:

int **a=new int*[2];

for (int i=0; i<2; i++) {

A[i]=new Int[3];

}

////////////////////////////

for (int i=0; i<2; i++) {

Delete[] a[i];

}

Delete[] A;

Pointers to two-dimensional arrays:

int **p=a; Pointer P equals a

*p <=> a[0];  * (p+i) <=> a[i]; * (* (p+i) +j) <=> A[i][j]

Row and column calculations for two-dimensional arrays:

Total size: t=sizeof (a)/sizeof (A[0][0])

Number of columns: n=sizeof (a[0])/sizeof (a[0][0]); Array[0].size ()//vector type requires deep "inside" to find the size of the column

Number of rows: m=t/n; Array.size ()//vector type can directly find the size of the row

Two-dimensional arrays

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.