How do you erase *AND CONTINUE* using a std::reverse_iterator?

 ̄綄美尐妖づ 提交于 2019-11-29 04:13:58

It should just be

std::list<int>::reverse_iterator it = list.rbegin();

while(  it != list.rend() )
{
   int value=*it;
   if( some_cond_met_on(value) )
   {     
        ++it;
        it= reverse_iterator(list.erase(it.base()); // change to this!
   }
   else
   {
     ++it;
   }
}

Most erase() implementations I have seen return the next iterator in the sequence for exactly this kind of situation, eg:

std::list<int>::reverse_iterator it = list.rbegin();
while( it != list.rend() )
{
    int value = *it;
    if( some_cond_met_on(value) )
    {
        it = list.erase( it );
    }
    else
    {
        ++it;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!