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

后端 未结 13 1043
长情又很酷
长情又很酷 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:36

    Use std::remove_if algorithm.

    Edit: Work with collections should be like: 1. prepare collection. 2. process collection.

    Life will be easier if you won't mix this steps.

    1. std::remove_if. or list::remove_if ( if you know that you work with list and not with the TCollection )
    2. std::for_each

提交回复
热议问题