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

后端 未结 13 1029
长情又很酷
长情又很酷 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.

提交回复
热议问题