C ++ multidimensional arrays knowledge Summary

Epidemic force, hope to see you stick to this way of learning. At the same time recording the hardships of finding a job is not easy in the process. Blog content continued to increase and decrease changes are excerpts and personal understanding. Welcome all criticism which the error, mutual encouragement and common progress.

First, the initialization of multidimensional arrays

For an array of three rows and four columns will be appreciated by 3 array of size 4 constituted, both arrays multidimensional arrays of arrays. The definition of a multidimensional array as follows:

int a[3][4];                      //定义大小为3 x 4的整型数组
int a[3][4] = {0};                //定义大小为3 x 4的整型数组,并所有赋初值0
int a[3][4] = {{0},{1},{2}};      //定义大小为3 x 4的整型数组,每一行第一个元素分别为0,1,2
int a[3][4] = {0,1,2};            //定义大小为3 x 4的整型数组,第一行前三个元素为0,1,2

Multidimensional array index references
use a reference note to change the code, wherein a two-dimensional array of an element can be extracted

int arr[3][4][5];
int a[5][5];
a[2][3] = arr[0][0][0];          //对多维数组的元素单独赋值
int (&b)[5] = a[1];              //把b绑定到a的第二个5元数组上

Sample code:

const int row = 3;
const int col = 4;
int arr[row][col];
for (int i = 0; i < row; i++)
	for (int j = 0; j < col; j++)
		arr[i][j] = col*i + j;
int(&temp)[col] = arr[1];           //temp为arr第二个大小为4的数组
cout << temp[0] << endl;            //输出结果为4

Sample Code II:
The following code, we use the range for two-dimensional array assignment statement to achieve. It is noteworthy that, if you need to read and write the array elements, then all layers reference cycle shall be added. If the read-only, then in addition to the innermost loop without any increase in references, plus all the remaining layer reference, or will be error. (Not to analyze the specific reasons)

const int row = 3;
const int col = 4;
int arr[row][col];
int a = 0;
for (auto &i : arr)                 //利用范围for语句实现赋值
	for (auto &j : i)               //注意此处i和j都是引用
		j = a++;
cout << arr[1][0] << endl;          //输出结果为4

Second, multi-dimensional arrays and pointers

In an array of chapters we have analyzed and Meanings There must master pointer and array combined. In this chapter we will combine multidimensional array, do it again review summary.
Sample code:
Note the difference between comparative analysis and two-dimensional array of one-dimensional array, where not too much use of the following explanation of the embodiment, want to know their analysis.

//二维数组
const int row = 3;
const int col = 4;
int arr[row][col];
int a = 0;
for (auto &i : arr)
	for (auto &j : i)
		j = a++;
int (*ip1)[4] = arr;            //指向arr第一行数组的指针
int (*ip2)[4] = &arr[0];        //指向arr第一行数组的指针
cout << *(ip1 + 1)[0] << endl;  //输出结果为4
cout << *(ip2 + 1)[0] << endl;  //输出结果为4
//一维数组
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
int(*p1)[10] = &a;
int(&p2)[10] = a;
int *p3 = a;
cout << (*p1)[1] << endl;             //输出结果为1
cout << (*(p1 + 1))[0] << endl;       //输出结果为-858993460
cout << (*p1 + 1)[0] << endl;         //输出结果为1
cout << p2[1] << endl;                //输出结果为1
cout << *(p3+1) << endl;              //输出结果为1
Published 27 original articles · won praise 4 · Views 4516

Guess you like

Origin blog.csdn.net/SGL_LGS/article/details/104637697