In practice, the vector series in c ++ -- the second worst iterator failed (deleting elements in the vector)

Source: Internet
Author: User

In practice, the vector series in c ++ -- the second worst iterator failed (deleting elements in the vector)

Directly refer to the Code:

# Include

  
   
# Include

   
    
Using namespace std; int main () {vector

    
     
V; v. push_back (1); v. push_back (2); v. push_back (3); std: vector

     
      
: Iterator iter1 = v. begin () + 2; v. erase (v. begin () + 1); int n = * iter1; return 0 ;}

     

    

   

  

After erase, The iterator becomes invalid.

Several Failures of the vector iterator:
1. When an element is inserted (push_back), The iterator returned by the end operation is definitely invalid.
2. When an element is inserted (push_back), the return value of capacity is different from that before the element is inserted. Then, the entire container needs to be reloaded, And the iterator returned by the first and end operations will become invalid.
3. After the delete operation (erase, pop_back) is performed, all the iterators pointing to the delete vertex are invalid. All the iterators pointing to the elements behind the delete vertex are also invalid.

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.