Boost logo

Boost Users :

From: Douglas Paul Gregor (gregod_at_[hidden])
Date: 2003-11-30 16:39:06


On Sun, 30 Nov 2003 John.Wismar_at_[hidden] wrote:

>
> Here's the situation I'm trying to remedy, boiled down:
>
> class A
> {
> public:
> int size() const;
> };
>
> class B
> {
> public:
> int stretch_it(int size) const;
> int get_total() const;
> private:
> std::vector<A> myVec;
> };
>
> int B::get_total() const
> {
> int retVal = 0;
> for (std::vector<A>::const_iterator it = myVec.begin(); it != myVec.end
> (); ++it)
> {
> retVal += stretch_it(it->size());
> }
> return retVal;
> }
>
> I'd love to use std::accumulate() or equivalent instead of a hand-coded
> loop, but I don't know if it's possible to specify a combination of
> operations like I have here.... Or if the result would be readable!

You can replace the body of get_total with:

return std::accumulate(myVec.begin(), myVec.end(), 0,
                       boost::bind(std::plus<int>(),
                                   _1,
                                   boost::bind(&B::stretch_it,
                                               this,
                                               boost::bind(&A::size,
                                                           _2))));

You can decide whether that's better than the hand-coded loop or not :)

        Doug


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