Boost logo

Boost Users :

From: dmoore99atwork (dmoore_at_[hidden])
Date: 2001-12-14 15:06:59


Hello,

I'm trying to explore the compose, bind, and mem_fn libraries. I
used a "real" problem I'm having. Given a vector<Trade>, I want to
gather a sum of the return values of Trade::volume().

I wound up writing "accumulate_with_op" thusly:

template <class _InputIterator, class _Tp, class _UnaryOperation>
_Tp accumulate_with_op(_InputIterator first, _InputIterator last, _Tp
Init, _UnaryOperation unary_op)
{
        for ( ; first != last; ++first)
                Init = Init + unary_op(*first);
        return Init;
}

And I can call it like this:

vector<Trade> li;
// .....
sum = accumulate_with_op(li.begin(),li.end(),0,mem_fun_ref
(&Trade::volume))

This gives me my sum pretty nicely, so I'm sort of happy... BUT...

I tried for a long time to use combinations of for_each,
compose_f_gx, and mem_fn to be able to do the Sum (to lead to other
things like mean, stddev) into a stateful function object.
Something like this:

/* broken code, trying to convery my intentions */
MeanValue m1;
m1 = for_each(l.begin(), l.end(),compose_f_gx(MeanValue(),mem_fun_ref
(&Trade::volume));
cout << m1.result();

I wanted my MeanValue::operator() to be called with the return value
from my member function, and I wanted to be able to "capture" the
result in a function object i could interrogate afterwards.

1. Am I barking up the wrong tree? accumulate_with_op was pretty
easy to write, but my problem seemed straighforward enough to be
solved with standard c++ or Boost building blocks.
2. Or I am just using the wrong compiler (MSVC6 SP5)?

Thanks,
Dave


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