Consider the following non-working code:
typedef map<int, unsigned> mymap;
mymap m;
for( int i = 1; i < 5; ++i )
m[i] = i;
// 'remove' all elements from map where .second < 3
remove_if(m.begin(), m.end(), bind2nd(less<int>(), 3));
I'm trying to remove elements from this map where .second < 3
. This obviously isn't written correctly. How do I write this correctly using:
bind
+ less<>
but without having to write a custom functorI know I'm not erase
ing the elements. Don't worry about that; I'm just simplifying the problem to solve.
I'm not sure how to do this using just the STL binders but I think your main problem is that what's being passed into the functor you give to remove
isn't just an int
but a pair<int, unsigned>
.
Using boost::bind you'd do it like this:
remove_if(m.begin(), m.end(), bind(&std::pair<int, unsigned>::second, _1) < 3);
Using a lambda function it's something like this:
remove_if(m.begin(), m.end(), [](const std::pair<int, unsigned>& p) { return p.second < 3; } );
I haven't checked that this compiles, sorry.