Removes the specified element from the vector

① delete only one element:

vector < int > NUM; 

for (vector < int > :: = num.begin ITER Iterator ();! = num.end ITER (); ITER ++) {         // delete the specified one element from the vector 
    IF (* ITER == K) { 
        num.erase (ITER); 
        BREAK ; 
    } 
}

 

② deleting the specified plurality of distinct elements: Returns the erase function is an iterator to the next element of the element is deleted , the execution erase () after the iterator should minus 1, a point in front of

for (Vector < int !> :: = num.begin ITER Iterator (); ITER num.end = (); ITER ++ ) {
         IF (ITER == * K) { 
            ITER = num.erase (ITER); 
            ITER - ;                     // ERASE function returns an iterator to point the next element of the current element is deleted 
    } 
}

Guess you like

Origin www.cnblogs.com/shiliuxinya/p/12219149.html