"Read Primer" 20.<3.6> multidimensional array Page112

Source: Internet
Author: User

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

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.