如何使用std :: reverse_iterator擦除* AND CONTINUE *? [英] How do you erase *AND CONTINUE* using a std::reverse_iterator?

查看:160
本文介绍了如何使用std :: reverse_iterator擦除* AND CONTINUE *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经上下Stackoverflow,甚至非常,非常好的博士Dobbs文章,但我找不到问题的确定答案。

I've been up and down stackoverflow and even the very, very nice Dr. Dobbs article but I can't find a definitive answer to the question.

问题的答案的一部分 std :: reverse_iterator的缺点是什么?说这可能是不可能的。 p>

A section of the answer to the question What are the shortcomings of std::reverse_iterator? says that it might not be possible at all.


std::list::reverse_iterator it = list.rbegin();

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



PS:我知道还有其他选择作为erase_if(),但我正在寻找这个具体问题的答案。

PS: I do know there are other alternatives, such as erase_if(), but I'm looking for an answer to this specific question.

推荐答案

>

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;
   }
}

这篇关于如何使用std :: reverse_iterator擦除* AND CONTINUE *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆