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:
- Standard STL function objects & techniques using
bind+less<>but without having to write a custom functor - Boost.Bind
- C++0x Lambdas
I know I’m not eraseing 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
removeisn’t just anintbut apair<int, unsigned>.Using boost::bind you’d do it like this:
Using a lambda function it’s something like this:
I haven’t checked that this compiles, sorry.