Re: Problems removing an element from a std::set using a reverse_iterator
In article <1185287381.614903.61710@m3g2000hsh.googlegroups.com>,
irotas <google@irotas.net> wrote:
for(UIntSet::reverse_iterator ri = uis.rbegin() ;
ri != uis.rend() ;)
{
if((*ri) < THRESHOLD)
{
break;
}
uis.erase(--(ri.base()));
}
Safer and equivalent to your code is
UIntSet::reverse_iterator ri = std::find_if
(
uis.rbegin(),
uis,rend(),
std::bind2nd
(
std::less<unsigned int>(),
THRESHOLD
)
);
// you now erase [uis.rbegin(),ri)
// ri.base points to next in forward sequence and uis.rbegin()
// points to r.end() that is you want to erase [ri.base(),uis.end())
// or
uis.erase(ri.base(),uis.end())
This should work with sequence containers of sorted data and
sorted assoc. containers.
I would expect that a vector<unsigned int> that is not sorted to
compare favorably if not surpass greatly the efficiency of set to
test just the erasure process.
std::vector<unsigned int> vec;
// fill vec
vec.erase
(
remove_if(vec.begin(),vec.end(),erase_me()),
vec.end()
);
is even more concise, this involve N compares and # to remove copies
and # to remove dtor calls. The copies and dtors are dirt cheap. so it
is N compares and they are cheap too most likely.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]