Can you remove elements from a std::list while iterating through it?

后端 未结 13 875
长情又很酷
长情又很酷 2020-11-22 06:30

I\'ve got code that looks like this:

for (std::list::iterator i=items.begin();i!=items.end();i++)
{
    bool isActive = (*i)->update();
    /         


        
相关标签:
13条回答
  • 2020-11-22 06:52

    You want to do:

    i= items.erase(i);
    

    That will correctly update the iterator to point to the location after the iterator you removed.

    0 讨论(0)
  • 2020-11-22 06:53

    You need to do the combination of Kristo's answer and MSN's:

    // Note: Using the pre-increment operator is preferred for iterators because
    //       there can be a performance gain.
    //
    // Note: As long as you are iterating from beginning to end, without inserting
    //       along the way you can safely save end once; otherwise get it at the
    //       top of each loop.
    
    std::list< item * >::iterator iter = items.begin();
    std::list< item * >::iterator end  = items.end();
    
    while (iter != end)
    {
        item * pItem = *iter;
    
        if (pItem->update() == true)
        {
            other_code_involving(pItem);
            ++iter;
        }
        else
        {
            // BTW, who is deleting pItem, a.k.a. (*iter)?
            iter = items.erase(iter);
        }
    }
    

    Of course, the most efficient and SuperCool® STL savy thing would be something like this:

    // This implementation of update executes other_code_involving(Item *) if
    // this instance needs updating.
    //
    // This method returns true if this still needs future updates.
    //
    bool Item::update(void)
    {
        if (m_needsUpdates == true)
        {
            m_needsUpdates = other_code_involving(this);
        }
    
        return (m_needsUpdates);
    }
    
    // This call does everything the previous loop did!!! (Including the fact
    // that it isn't deleting the items that are erased!)
    items.remove_if(std::not1(std::mem_fun(&Item::update)));
    
    0 讨论(0)
  • 2020-11-22 06:53

    do while loop, it's flexable and fast and easy to read and write.

    auto textRegion = m_pdfTextRegions.begin();
        while(textRegion != m_pdfTextRegions.end())
        {
            if ((*textRegion)->glyphs.empty())
            {
                m_pdfTextRegions.erase(textRegion);
                textRegion = m_pdfTextRegions.begin();
            }
            else
                textRegion++;
        } 
    
    0 讨论(0)
  • 2020-11-22 06:55

    The alternative for loop version to Kristo's answer.

    You lose some efficiency, you go backwards and then forward again when deleting but in exchange for the extra iterator increment you can have the iterator declared in the loop scope and the code looking a bit cleaner. What to choose depends on priorities of the moment.

    The answer was totally out of time, I know...

    typedef std::list<item*>::iterator item_iterator;
    
    for(item_iterator i = items.begin(); i != items.end(); ++i)
    {
        bool isActive = (*i)->update();
    
        if (!isActive)
        {
            items.erase(i--); 
        }
        else
        {
            other_code_involving(*i);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:56

    You have to increment the iterator first (with i++) and then remove the previous element (e.g., by using the returned value from i++). You can change the code to a while loop like so:

    std::list<item*>::iterator i = items.begin();
    while (i != items.end())
    {
        bool isActive = (*i)->update();
        if (!isActive)
        {
            items.erase(i++);  // alternatively, i = items.erase(i);
        }
        else
        {
            other_code_involving(*i);
            ++i;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:56

    Removal invalidates only the iterators that point to the elements that are removed.

    So in this case after removing *i , i is invalidated and you cannot do increment on it.

    What you can do is first save the iterator of element that is to be removed , then increment the iterator and then remove the saved one.

    0 讨论(0)
提交回复
热议问题