简体   繁体   中英

c++ erase the std::vector.end() while looping over all iterators

I want to erase some iterator from a vector, so this is what I have now.

void function(std::vector <class*> & vector)
{
    std::vector <class*>::iterator it;
    for(it = vector.begin(); iter != vector.end(); ++iter)
        {
            if(it->value == 1)
                vector.erase(it);
        }
    Display(vector);
    return;
}

Apparently this code gives me an error when the iterator got removed is the last one in the vector, otherwise seems it's working fine. I know it might not be desirable behavior to modify vector inside such a loop, but if I have to do this, what will be the best way?

Thanks.

for (it = vector.begin(); it != vector.end(); )
{
    if (it->value == 1)
        it = vector.erase(it);
    else
        ++it;
}

But for this case, you should actually just use std::remove_if with an appropriate predicate.

Another approach:

vector.erase( std::remove_if( vector.begin(), vector.end(), boost::bind( &class::value, _1 ) == 1 ), vector.end() );

boost::bind can probably be replaced by std::bind if it's available.

It's bad idea to erase from vector while iterate over it. Simply filter it.

void function(std::vector <class*> & vector)
{
    std::vector <class*>::iterator from= vector.begin();
    std::vector <class*>::iterator to= from;
    for(; from != vector.end(); ++from)
        {
            if((*from)->value == 1) continue; 
            // it should be (*from) or use boost::ptr_vector
            *(to++)=*from;
        }
    vector.erase( to, vector.end() );
    Display(vector);
    return;
}

This is functionality exactly identical as code by Ylisar. IMHO this is best for vector, if you always have something to remove, but if remove is very rare (for whole one vector), use Benjamin Lindley version.

Whatever optimization may be, you may filter only if it you have something to erase:

void function(std::vector <class*> & vector)
{
   std::vector <class*>::iterator to= vector.begin(); 
   for(; to != vector.end(); ++to)
   {
      if((*to)->value == 1) 
      {
         std::vector <class*>::iterator from=to;
         for(++from; from != vector.end(); ++from)
         {
            if((*from)->value == 1) continue;
            *(to++)=*from;
         }
         vector.erase( to, vector.end() );
         break;
      }
    }
    Display(vector);
    return;
}

If you don't need to preserve order, you may copy from back to minimal copy overheat:

void function(std::vector <class*> & vector)
{
   std::vector <class*>::iterator to= vector.begin(); 
   std::vector <class*>::iterator from=vector.end();
   if(to == from) return;
   for(;;)
   {
      if((*to)->value == 1) // need skip value from begin
      {
         for( --from; from != to; --from)
         {
            if((*from)->value == 1) continue; // need skip value from end
            *to=*from;
            ++to; // copied, move to next
         }
      } 
      else 
      {
         ++to; //preserved, move to next
      }
      if(to == from) 
      {
         vector.erase( to, vector.end() ); 
         break;
      }
    }
    Display(vector);
    return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM