I have a std :: list of objects. The list is sorted and should remain so. I need to find those objects that satisfy a certain criterion (I have a predicate for this), pass them to a function, and then remove the objects from the list.
It's not so difficult to write a loop that calls std::find_if() , calls the operation on its result (if any), calls list.erase() and passes the result of this as a begin iterator for the next call to std::find_if() , IME however, people find this code harder to read than to write.
Therefore, I would prefer to use some algorithms from std lib instead of writing my own loop.
One idea was to (ab) use std::list<>::remove_if() : invoke an operation on elements that match _f from the predicate before it returns true so that the list removes the elements. Will it be standard? (The elements themselves will not be changed, but only the data to which they refer.)
Or can you come up with a better solution? (Again, the main goal is to make it easy to read and understand.) Maybe because I just ran into it, but it seems to me that this cannot be an unusual usage pattern for a sequence of objects.
Note. At the moment, we are firmly stuck in the land of C ++ 03 :-/ C ++ 11/14/17 solutions will be interesting and thus welcome, but I need something that works with C ++ 03.
source share