Use Vector to create a two-dimensional array

Source: Internet
Author: User

Additional knowledge:

The resize function of the vector is to redefine the size of the array.

Create 3*3 arrays below

Method 1:

 
# Include <iostream> # include <vector> using namespace STD; int main () {int I, j; vector <int> array (3 ); // note that there is a space after the vector <> for (I = 0; I <3; I ++) array [I]. resize (3); for (I = 0; I <3; I ++) for (j = 0; j <3; j ++) array [I] [J] = I * j; for (I = 0; I <3; I ++) {for (j = 0; j <3; j ++) cout <array [I] [J] <""; cout <Endl;} return 0 ;}

 

Method 2 (initialize upon definition ):

# Include <iostream> # include <vector> using namespace STD; int main () {int I, j; vector <int> array (3, vector <int> (3); // you do not need to re-define the size after this definition. If you change the array size, change for (I = 0; I <3; I ++) for (j = 0; j <3; j ++) array [I] [J] = I * j; for (I = 0; I <3; I ++) {for (j = 0; j <3; j ++) cout <array [I] [J] <""; cout <Endl;} return 0 ;}

In fact, the outer layer size does not need to be defined, and the undefined extension with push_back is the most powerful place for vector to create a two-dimensional array.

 

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.