C + +: Erase () function of the container (container)

Source: Internet
Author: User

The erase () function of the container (container), which deletes an element, moves the pointer of the iterator and points to the next element;

An iterator (iterator) is a pointer to an assigned address that points to the same element;

Code:

* * * test.cpp * *  Created on:2014.04.18 * 
 author:spike/
      
/*eclipse CDT, gcc 4.8.1* /
      
#include <iostream>  
#include <vector>  
      
using namespace std;  
      
int main () {  
    Std::vector<int> VI = {1, 6, 6, 3};  
    Std::vector<int>::iterator Iter1;  
    Std::vector<int>::iterator Iter2;  
      
    Less delete a 6 for  
    (Iter1 = Vi.begin (); Iter1!= vi.end (); ++iter1) {  
        if (6 = *iter1) {  
            iter2 = iter1;  
            Vi.erase (iter2);  
            --iter1  
        }  
    }  
      
    Std::cout << "vector size is:" << vi.size () << Std::endl;  
    for (Iter1 = Vi.begin (); Iter1!= vi.end (); ++iter1) {  
        std::cout << *iter1 << "";  
    }  
    Std::cout << Std::endl;  
      
    return 0;  
}

Output:

Vector size Is:3  
1 6 3

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.