Boost logo

Boost Users :

From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2006-02-01 06:30:09


On 2/1/06, Daniel Lidström <daniel.lidstrom_at_[hidden]> wrote:
> Hello!
>
> I'm trying to use Boost.Lambda to count the total number of elements
> in a list of lists. This example shows what I'm trying to do:
>
> #include <boost/lambda/lambda.hpp>
> #include <algorithm>
> #include <list>
>
> using namespace std;
> using namespace boost::lambda;
>
> int main()
> {
> int count = 0;
> list<list<int> > ll;
> for_each(ll.begin(), ll.end(), var(count)+=(_1).size());
> }
>
> This will not compile: (13): error C2039: 'size': is not a member of
> 'boost::lambda::lambda_functor<T>'
> Is this possible to do?
> Thanks in advance!
>
> Hälsningar,
> Daniel
>

You'll need to use lambda's bind to access the size member function:

   for_each(ll.begin(), ll.end(), var(count)+=bind(&list<int>::size, _1));

You might might find std::accumulate a better match for your requirements:

   count = accumulate(ll.begin(), ll.end(), 0, _1 + bind(&list<int>::size, _2));

Stuart Dootson


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