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

后端 未结 13 1086
长情又很酷
长情又很酷 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条回答
  •  闹比i
    闹比i (楼主)
    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++;
        } 
    

提交回复
热议问题