Search code examples
c++vectorerase

Erasing last element of vector containter


I've got some problem with segmentation fault while I want to remove element from vector container which is the latest one pushed in.

vector<client> :: iterator it;

for(it=ktab->begin(); it!=ktab->end(); ){
    if(it->KEY_w==key_w) ktab->erase( it );
    ++it;
}

How to cope with this problem?


Solution

  • vector<client> :: iterator it;
    
    for(it=ktab->begin(); it!=ktab->end(); ){
        if(it->KEY_w==key_w) it = ktab->erase( it );
        else  ++it;
    }
    

    Though in such situations it is better to use member function erase with standard algorithm std::remove_it