remove_if
will not work with associative containers. But remove_copy_if
may work but at the expense of copying your map. Instead I'll do it with count_if
.
1) Standard STL function objects & techniques using bind + less<> but without having 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 want remove_if behavior you'll need to roll your own algorithm. I don't believe there are any 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;
}