Stl--vector.erase () Usage

Source: Internet
Author: User

Today, using the Vector.erase () method, found a lot of ghost animals. Later, the data was found, erase () is to delete the current iterator to return the next iterator. If you just use erase but do not update the iterated iterator will cause the current iterator to be released into a wild pointer, and then use + + after the wild pointer-the operation will certainly be problematic. So you need to update the iterator, ITER = Myvec.erase (); After the update, ITER has pointed to the next bit of the deleted element. At this point out of judgment, and back to the for loop, the for loop ITER and + + once, so that the latter element is skipped. So, after the use of ITER = Myvec.erase (), a decisive and clever--iter is needed to make ITER return to its proper position.

There is no solution to the problem, and how the length of the vector changes when erase is used. When erase is used, the elements behind the vector should be moved forward. The length should be the original length-1. Specifically want to know the results, write a short program to verify your own OK.

Stl--vector.erase () Usage

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.