A multi-bit array in C + +, which is strictly an array of arrays.
int ia[3[4// size 3 array, each element is an array with 4 integers // An array of size 10, Each element is an array of size 20,// The elements of these arrays are arrays of 30 integers int arr[[ [+] = {0// Initialize all elements to 0
Initializing a multidimensional array
intia1[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,Ten, One} }; //The following statement is equivalent to the above initialization intia2[3][4] = {0,1,2,3,4,5,6,7,8,9,Ten, One};
Subscript References for multidimensional arrays
//iterate over multidimensional arrays with multiple loops for(size_t i =0; I! =3; ++i) { for(size_t j =0; J! =4; ++j) {cout<<"Row is"<< I <<", Col is"<< J <<", value is:"<< Ia1[i][j] <<Endl; } } //use the scope for statement to traverse for(Auto &row:ia1) { for(Auto &Col:row) {cout<<"value is:"<< Ia1[i][j] <<Endl; } }
Pointers and multidimensional arrays
The declaration of a level two element in a multidimensional array requires extra attention, and of course we can use auto instead of this declaration, or use typedef to make a declaration multiple times.
int ia[3[4]; // an array of size 3, each containing an array of 4 integers int (*p) [4// P points to an array containing 4 integers (pointers to arrays) // int *p[4]; // an array of integer pointers (arrays of pointers) p = &ia[2]; // p points to the tail element of the IA
All the code for this section
#include <iostream>usingstd::cout;usingStd::endl;intMain () {intia[3][4];//an array of size 3, each containing an array of 4 integers//an array of size 10, with each element being an array of size 20,//The elements of these arrays are arrays that contain 30 integers intarr[Ten][ -][ -] = {0};//initialize all elements to 0 intia1[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,Ten, One} }; //The following statement is equivalent to the above initialization intia2[3][4] = {0,1,2,3,4,5,6,7,8,9,Ten, One}; //iterate over multidimensional arrays with multiple loops for(size_t i =0; I! =3; ++i) { for(size_t j =0; J! =4; ++j) {cout<<"Row is"<< I <<", Col is"<< J <<", value is:"<< Ia1[i][j] <<Endl; } } //use the scope for statement to traverse through the auto type to simplify the code for(Auto &row:ia1) { for(Auto &Col:row) {cout<<"value is:"<< Col <<Endl; } } intia3[3][4];//an array of size 3, each containing an array of 4 integers int(*p) [4] = IA3;//p points to an array containing 4 integers (pointers to arrays)//int *p[4]; //an array of integer pointers (arrays of pointers)p = &ia3[2];//p points to the tail element of the IA }
"Read Primer" 20.<3.6> multidimensional array Page112