Note points for using vectors

Source: Internet
Author: User

When using vectors in the STL,

1, traverse the data and at the same time to modify the case, slightly inattentive, will appear "Vector iterators incompatible!".

For example, the following code:

vector<int> list;  for (vector<int>::iterator it = List.begin (); it! = List.end (); ++it)    {if1 )         list.erase (it);}

If it is in the final position and performs a erase operation, the assertion occurs when compared to end () because it is not updated.

The correct method should be as follows:

vector<int> list;  for (vector<int>::iterator it = List.begin (); it! = list.end ();) {    if1)          = List.erase (it);      Else         ++it;}

After all container classes in the STL have performed the erase operation, a iterator is returned, pointing to the successor of the currently deleted element, or end ().
Therefore, after the delete operation is performed, the value returned by the application modifies the iteration element.

2, the empty vector cannot perform the begin operation, otherwise "vector iterators incompatible!" will also appear.

Note points for using vectors

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.