If the moved items can stay where they are in first , just use copy_if with move_iterator .
std::copy_if(std::make_move_iterator(first.begin()), std::make_move_iterator(first.end()), std::back_inserter(second), cond);
If moved items should be removed from first , I would do
// partition: all elements that should not be moved come before // (note that the lambda negates cond) all elements that should be moved. // stable_partition maintains relative order in each group auto p = std::stable_partition(first.begin(), first.end(), [&](const auto& x) { return !cond(x); }); // range insert with move second.insert(second.end(), std::make_move_iterator(p), std::make_move_iterator(first.end())); // erase the moved-from elements. first.erase(p, first.end());
Or partition_copy with move_iterator followed by an assignment:
std::vector<T> new_first; std::partition_copy(std::make_move_iterator(first.begin()), std::make_move_iterator(first.end()), std::back_inserter(second), std::back_inserter(new_first), cond); first = std::move(new_first);
TC
source share