nyarlathotep108 nyarlathotep108 - 21 days ago 5
C++ Question

How to (re)move from a container if predicate is satisfied?

How can I move the element into

aux_list
using STL algorithm module?

std::remove_copy_if( list.begin( ), list.end( ), std::back_inserter( aux_list ),
[]( const value_type& item_ptr )
{
return !item_ptr->is_valid( );
} );

Answer

Use move iterators:

std::remove_copy_if(
    std::make_move_iterator(list.begin()),
    std::make_move_iterator(list.end()),
    std::back_inserter(aux_list),
    [](const value_type& item_ptr) { return !item_ptr->valid(); });

Note that the elements still all exist in list - they weren't actually removed (this is a weirdly named algorithm - think of it as copy_unless()). So now you will have a bunch of elements in an unspecified state.