Multi-dimensional array

Source: Internet
Author: User

Multi-dimensional array

Strictly speaking, the C ++ language does not contain multidimensional arrays. Generally, multidimensional arrays are actually arrays. Keeping this in mind will be of great benefit to understanding and using multi-dimensional arrays in the future.

 

Use the range for statement to process multi-dimensional arrays

Two-layer nested for loop to process elements of multi-dimensional arrays:

Constexptr size_t rowcnt = 3, colcnt = 4;

Int Ia [rowcnt] [colcnt];

For (size_t I = 0; I! = Rowcnt; ++ I)

For (size_t J = 0; J! = Colcnt; ++ J)

IA [I] [J] = I * colcnt + J;

The range for statement is added to the new C ++ 11 standard, so the previous program can be simplified as follows:

Size_t CNT = 0;

For (Auto & row: IA)

For (Auto & Col: Row)

{

Col = CNT;

++ CNT;

}

The value assigned to the IA element by this loop is exactly the same as that in the previous loop. The difference is that the task of managing the array index is handed over to the system by using the range for statement. To change the element value, declare the control variables row and Col as reference types. The first for loop traverses all the elements of IA. These elements are arrays of 4 sizes. Therefore, the row type should be a reference to an array containing 4 integers. The second for loop traverses one of the four element arrays, so the col type is an integer reference. In each iteration, the CNT value is assigned to the current element of IA, and 1 is added to CNT.

In the above example, because we want to change the value of the array element, we chose the reference type as the Circular control variable, but there is still a deep-seated reason for us to do this. For example, consider the following loop"

For (const auto row: IA) // for each element of the outer Array

For (Auto Col: Row) // for each element of the inner Array

Cout <Col <Endl;

There is no write operation in this loop, but we declare the control variable of the outer loop as the reference type to avoid the array being automatically converted to a pointer. If no reference type is required, the cycle is shown in the following form:

For (Auto row: IA)

For (Auto Col: Row)

The program cannot be compiled. This is because, as before, the first loop traverses all the elements of IA. Note that these elements are actually 4 arrays. Because the row is not a reference type, the compiler will automatically replace these array elements with pointers to the first element in the array when initializing the row. The row type obtained in this way is int *. Obviously, the loop in the inner layer is invalid. The compiler will try to traverse in int *, which is obviously far from the program's original intention.

To use the range for statement to process multi-dimensional arrays, the control variables of all loops except the inner loop should be of the reference type.

 

Pointers and multi-dimensional arrays

When the program uses a multi-dimensional array name, it will automatically convert it into a pointer to the first element of the array.

Because multi-dimensional arrays are actually arrays, the pointer converted from the multi-dimensional array name actually points to the first internal array:

Int Ia [3] [4];

INT (* P) [4] = IA; // P points to an array containing four Integers

P = & Ia [2]; // P points to the end element of IA

We first define (* P) that p is a pointer. Next, observe the right side and find that the pointer P refers to an array with a dimension of 4. On the left side of the observation, the elements in the array are integers. Therefore, p is the pointer to an array containing four integers.

In the preceding statement, parentheses are required:

Int * IP [4]; // array of integer pointers

INT (* IP) [4]; // point to an array containing four Integers

With the introduction of the new C ++ 11 standard, using auto or decltype can avoid adding a pointer type to the array as much as possible:

// Output the value of each element in Ia. Each inner array occupies one row.

// P points to an array containing four Integers

For (Auto P = IA; P! = Ia + 3; ++ P)

For (Auto q = * P; Q! = * P + 4; ++ q)

Cout <* q <'';

The for loop of the outer layer first declares a pointer P and points it to the first inner layer array of IA, and then iterates until all three rows of IA are processed. The incremental operation + + P moves the pointer P to the next row of IA.

The for loop of the inner layer outputs the values contained in the inner array. It first points the pointer Q to the first element of the row where p is currently located. * P is an array containing four integers. As usual, the array name is automatically converted to a pointer pointing to the first element of the array. The inner for loop continues to iterate until we have processed all the elements in the current inner array. To obtain the termination condition of the inner for loop, the next time P is referenced, the pointer pointing to the first element of the inner array is obtained. If 4 is added to the element, the termination condition is obtained.

Of course, the same functions can be implemented using the standard library functions begin and end, and it looks concise:

For (Auto P = begin (IA); P! = End (IA); ++ P)

For (Auto q = begin (* P); Q! = End (* P); ++ q)

Cout <* q <'';

 

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.