remove_if will not work with associative containers. But remove_copy_if may work, but by copying your card. Instead, I will do it with count_if .
1) Standard functions and methods of the STL function using bind + less <> but without the need to write a custom functor
// I don't think it can be done with standard c++ without introducing new functors and adaptors. std::size_t count = std::count_if( m.begin(), m.end(), std::sgi::compose1( std::bind_2nd( std::less<int>(), 3 ), &boost::get<1,mymap::value_type> ) );
2) Boost.Bind
std::size_t count = std::count_if( m.begin(), m.end(), boost::compose_f_gx( &boost::bind( std::less<int>, _1, 3 ) &boost::get<1,mymap::value_type> ) );
3) C ++ 0x Lambdas
std::size_t count = std::count_if( m.begin(), m.end(), []( const mymap::value_type& item ) { return item.second < 3; } );
If you really need remove_if behavior, you need to collapse your own algorithm. I do not believe that there are modifying standard algorithms that work with associative containers.
template< typename FwdIter, typename AssocCont, typename Pred > std::size_t assoc_remove_if( FwdIter iter, FwdIter end, AssocCont& cont, Pred pred ) { std::size_t count = 0; while( iter != end ) { if( pred(*iter) ) { ++count; iter = cont.erase(iter); } else { ++iter; } } return count; }
source share