Vector series in practice c ++ -- using vector to construct two-dimensional arrays

Source: Internet
Author: User

Vector series in practice c ++ -- using vector to construct two-dimensional arrays

2D arrays are sometimes used, but few use vector to construct a 2D array.

First of all, it should be clear that there is no two-dimensional array in the computer world, just a concept of the user. In fact, our so-called two-dimensional array must also be a continuous memory.

In many cases, we can use a vector to represent a regular two-dimensional array, as long as the index corresponds to it.

So, if I leave it alone, I want to put a vector in the vector?

# Include

  
   
# Include

   
    
Using namespace std; int main () {vector

    
     
> Array (3); for (int I = 0; I <3; I ++) array [I]. resize (3); for (int I = 0; I <3; I ++) for (int j = 0; j <3; j ++) array [I] [j] = (I * j); for (int I = 0; I <3; I ++) {for (int j = 0; j <3; j ++) cout <

    

   

  

When "column" is used again, the resize method of vector must be used. Otherwise, [] cannot be used for access.

The reserve of vector cannot be used to allocate capacity here, because the previous blog has analyzed the cause.

In fact, what I want to say is that if you want to build a so-called two-dimensional array, the most important thing is to use resize to allocate capacity.

Related Article

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.