remove_if
連想コンテナでは機能しません。しかし、remove_copy_if
うまくいくかもしれませんが、マップをコピーすることを犠牲にして。代わりに、でそれを行いcount_if
ます。
1)bind + less <>を使用するが、カスタムファンクターを作成する必要のない標準のSTL関数オブジェクトと手法
// 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ラムダ
std::size_t count = std::count_if( m.begin(), m.end(),
[]( const mymap::value_type& item )
{ return item.second < 3; } );
remove_ifの動作が本当に必要な場合は、独自のアルゴリズムをロールする必要があります。連想コンテナで機能する変更標準アルゴリズムはないと思います。
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;
}