link: https: //zhuanlan.zhihu.com/p/23579860
Source: know almost
copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
statement:
- The content in the article is collected and compiled from "C++ Primer Chinese Edition (5th Edition)", and the copyright belongs to the original book.
- The original book has more detailed and wonderful interpretations, please buy genuine books for learning.
- This article is only for learning and communication, any form of reprinting is prohibited
Multidimensional arrays are actually arrays of arrays
int ia[3][4];
Read from the inside out: ia is an array containing 3 elements, and each element of it is an array containing 4 integers
initialization
int ia[3][4] = {
{
0, 1, 2, 3}, {
4, 5, 6, 7}, {
8, 9, 10, 11} };
In essence, there is no difference between the initialization of a one-dimensional array. ia has 3 elements, so there are 3 curly braces inside the outer curly braces, and each curly brace is used to initialize an element of ia.
Like a one-dimensional array, you can also give only a part of the initial value
int ix[3][4] = {
{
0}, {
4}, {
8} };
int ia[3][4] = {
0, 3, 6, 9 };
Subscript
ia[2][3] returns the third element of the second element (array) of ia
ia[1] returns the first element of ia (array)
Pay attention to understanding that multidimensional arrays are actually arrays of arrays.
Range for
You can use for loops and range for to traverse multidimensional arrays
When using the scope for, it is best to declare the control variables as reference types
size_t cnt = 0;
for( auto &row : ia )
{
for( auto &col : row )
{
col = cnt;
++cnt;
}
}
If not declared as a reference type
for( auto row : ia )
for( auto col : row )
The first line takes each element of ia, which is an array type, which will be converted to a pointer to the first element by the compiler, and an error will be reported in the second line
Therefore, only the innermost loop can not use reference types
For simplicity, it is best to use reference types
Pointers and multidimensional arrays
The array names of multidimensional arrays are also automatically converted to pointers
int ia[3][4];
auto p = ia;
Understanding process:
- ia is an array with 3 elements
- Each element of it is an array containing 4 elements.
- Therefore, its first element is also an array containing 4 elements
- The pointer to the first element is the pointer to the array with 4 elements
Equivalent to
int (*p)[4] = ia;
Loop through each element
for( auto p = ia; p != ia + 3; ++p )
{
for( auto q = *p; q != *p + 4; ++ q )
{
cout << *q << ' ';
}
cout << endl;
}
Dereference p to get an array containing 4 elements, *p is a pointer to its first element
You can also use begin and end functions
for( auto p = begin(ia); p != end(ia); ++p )
{
for( auto q = begin(*p); q != end(*p); ++ q )
{
cout << *q << ' ';
}
cout << endl;
}
Use type aliases to simplify writing
using int_array = int[4];
typedef int int_array[4];
Don't forget, the form of typedef is similar to variable declaration