Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-08-13 08:41:25


Hendrik Belitz wrote:
> Hello,
>
> I'm rather stucked in using lambda expressions for a presumably
> simple task. Suppose I have objects with a member function
> bool is_empty() const;
> stored in a vector: I want to count the number of objects for which
> their member functions returns false, which is simple, but not
> elegant to achieve by
> size_t sum = 0;
> for( Foo::const_iterator it = vec.begin(); it != vec.end(); ++it )
> if ( it->is_empty() == false ) sum++;

size_t sum = std::count_if( vec.begin(), vec.end(), !bind(
&GridCell::is_empty, _1 ) );

> I want to exchange this for a lambda expression, and came up with the
> following after some thinking.
>
> for_each( begin(), end(), if_then( !(_1 ->* &GridCell::is_empty)(),
> var(sum)++ ) );

Maybe another pair of parentheses will help?

    !((_1 ->* &GridCell::is_empty)())


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net