Re: Problems removing an element from a std::set using a reverse_iterator
In article <1185371762.105560.91760@g4g2000hsf.googlegroups.com>,
irotas <google@irotas.net> wrote:
However, if it's really
true that there's no good way to remove elements from a container
while iterating over it in reverse, that seems to be a severe language
limitation.
Just use iterators and decrement operations as appropriate:
UIntSet::iterator it(uis.end());
while(it !=uis.begin() && pred(*--it)) uis.erase(it++);
note there is no dereference of iterators pointing to erased items.
and no gyrations needed using reverse_iterator.
// easiest way to get the SFINAE class that tests for
// typedef ... key_type;
#include <boost/mpl/has_xxx.hpp>
BOOST_MPL_HAS_XXX_TRAIT_DEF(key_type)
template <class C,class B=has_key_type<C>::type>
struct erase_struct
{
typedef typename C::iterator iterator;
static iterator exec(C &c,iterator it)
{
// emulate recent drafts
c.erase(it++);
return it;
}
};
template <class C>
struct erase_struct<C,boost::mpl::false_>
{
typedef typename C::iterator iterator;
static iterator exec(C &c,iterator it)
{
return c.erase(it);
}
};
template <class C,class Pred>
void erase_backward_if(C &c,Pred pred)
{
typename C::iterator it(c.end());
while( it != c.begin() && pred(*--it))
erase_struct<C>::exec(c,it);
}
looks generic. but not tested.
note no dereferencing of invalid iterators in all cases.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]