Re: removing from set - does it have to be so ugly?
On 13 Nov., 03:19, Seungbeom Kim <musip...@bawi.org> wrote:
Vaclav Haisman wrote:
Yechezkel Mett wrote, On 12.11.2009 14:27:
On Nov 12, 10:47 am, Alfons <alfonsvanwor...@gmail.com> wrote:
[...]
or alternatively:
stuff_set.erase( std::remove_if( stuff_set.begin(), stuff_set.end(),
is_bad ),
stuff_set.end() );
The remove/erase idiom AFAIK does not work on std::set<> since std::set<> is
not a sequence.
Correct, but is it explicitly stated in the standard? If so, where?
I couldn't find a statement in the standard that prevents non-sequences
from being used with std::remove, other than the title of the subclause
"Mutating /sequence/ operations" (25.2), but it's not that none of the
algorithms in that section can be used with associative containers.
The C++03 standard is open in this point, but for C++0x it is pretty
clear that
std::remove_if( stuff_set.begin(), stuff_set.end(), is_bad );
won't be well-formed code, because as of [associative.reqmts]/6
"[..] For associative containers where the value type is the same as the
key type, both iterator and const_iterator are constant iterators."
but remove_if requires mutable iterator types ([alg.remove]/1):
"Requires: The type of *first shall satisfy the MoveAssignable
requirements[..]"
For that matter it is completely irrelevant whether the underlying
container is a sequence container or not: All "non-modifying"
algorithms should work for std::set and friends, because the
iterator range [stuff_set.begin(), stuff_set.end()) represents
a sequential view even for a non-sequence container.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]