I have a workflow in which I start with a std::list of movable objects and I want to traverse this list, MOVING elements which meet a certain condition (i.e. a unary predicate returns true) to a different std::list (e.g. using back_inserter to an existing list or even constructing a new list).
I've considered std::list::remove_if, but that just destructs elements if the condition is met, and returns the total number of elements removed -- but those elements are lost forever.
I've also considered std::remove_copy_if, but that COPIES the elements if the condition is not met and leaves the elements in the original list.
My hope/assumption was that splitting one list into two based on a predicate would be such a common use-case that std::list would have a member function to do it as a one-liner, or that a STL algorithm would be available to do it via iterators... but I'm not finding anything. Am I missing something obvious, or has this just not yet been implemented in the STL?
Pseudo-code for what I'm hoping the solution would look like is along the lines of this contrived "move_if" std::list member function:
std::list<T> source{ /* some objects of type T */ };
std::list<T> removed{};
source.move_if(std::back_inserter(removed), [&](const auto& e){ /* test e and return a bool */ });
or this one which constructs and returns a new list for the removed elements:
std::list<T> removed = source.move_if([&](const auto& e){ /* test e and return a bool */ });
If there isn't a canned solution already in the STL, is there a preferred idiom for doing this 'manually' (e.g. in a raw loop) that compilers are most likely able to optimize relative to other manual methods? And furthermore, is there a technical reason why this isn't already a supported library function or do I have an inflated view of how useful it would be to others? Thanks!