Vector earse的正确用法

#include  
#include  
using namespace std;  
int main()  
{  
    vector<int> array;    
    array.push_back(6);  
    array.push_back(3);  
    array.push_back(6);  
    array.push_back(6);  
    array.push_back(2);  

    vector<int>::iterator itor = array.begin();
    int nSize = array.size();
    int i = 0;
    for (int i =  0; i < nSize ;)
    {
        if (*itor == 6)
        {
            array.erase(itor);
            itor = array.begin() +  i;//这句是必须的,好像itor并不会自动指向下一个
            nSize--;
        }
        else
        {
            i++;
            itor++;
        }
    } 
    itor=array.begin();  
    for(itor=array.begin();itor!=array.end();)  
    {  
        cout<<(*itor++);  
    }  
    getchar();  
    return 0;  
}  

你可能感兴趣的:(C++,STL)