Re: how to iterator and delete elements in std::set
On 25 nov, 10:55, sasoon <alexander.georg...@gmail.com> wrote:
On Nov 22, 1:12 am, zl2k <kdsfin...@gmail.com> wrote:
hi, there
std::set<Obj> objs;
...
for (auto obj_iterator = objs.begin(); obj_iterator != objs.end(); =
+
+obj_iterator){
if (obj_iterator.do_you_want_to_be_erased() == true){
objs.erase(*obj_iterator);
}
}
Now I have the trouble since the iterator is destroyed after the first
erase. What is the proper way to do the job? Thanks.
I would use std::remove_if
http://www.sgi.com/tech/stl/remove_if.html
Would you ? Please, try it out before answering.
std::remove_if() copies the elements in the range such as removing the
unwanted value and returns the iterator to one past the last copied.
It doesn't work with std::set<> for two reason:
- the value are const and elements cannot be copied (in general)
- the internal structure is likely to be a RB-tree. copying the
values would make it a mess.
--
Michael