Boost logo

Boost Users :

Subject: Re: [Boost-users] std::accumulate with condition?
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2009-09-26 13:20:02


On Tue, Sep 22, 2009 at 3:11 PM, Archie14 <admin_at_[hidden]> wrote:

> If a given class defines operator + and there is a vector of instances of
> that
> class:
> how I can use std::accumulate to count sum against this vector for the
> elements that satisfy certain conditions?
> class A
> {
> public:
> A(useinsum) : _useinsum(useinsum) {};
> bool useinsum() {return _useinsum;}
> int operator + (int a) {return _i + a;}
> private
> bool _useinsum;
> int _i;
> }
>
> std::vector<A> lst;
>
> will that work?
> std::accumulate(lst.begin(), lst.end(), 0,
> boost::lambda::bind(&A::useinsum,
> boost::lambda::_1) == false)
>
>
Would filter iterators help you out?

http://www.boost.org/doc/libs/1_40_0/libs/iterator/doc/filter_iterator.html

- Rob.



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