Two-dimensional vector,clear () Please be careful, beware of the vector subscript out of range problem

Source: Internet
Author: User

Today we want to use vector to realize the function of two-dimensional array, and try to put the two-dimensional vector. Who knows when he got started, he met Ray.

The form of the code is roughly as follows:


VECTOR<VECTOR<INT>>VV (3);
Vv.clear ();
for (int i = 0; i < 3; i++)
	vv[i].push_back (0);

The vacuuming operation was performed because the two-dimensional vector was reused. However, there is a problem: vector subscript out of range



Later found in Single-step debugging, Clear () makes the size of the two-dimensional array 0, and the previously declared structure of the 3 one-dimensional array is corrupted.

When executing to the for header, the automatic window is as follows:


Make a change to the above code:


VECTOR<VECTOR<INT>>VV (3);
Vv.clear ();
Vv.resize (3);
for (int i = 0; i < 3; i++)
	vv[i].push_back (0);
The same is done to the for header when the automatic window is as follows: 
 
 
 
 

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.