|
Boost : |
From: Maxim Yegorushkin (e-maxim_at_[hidden])
Date: 2004-07-01 02:58:17
Dan <dan_at_[hidden]> wrote:
> I'm trying to use iter_fold with a vector_c to compute the sum of all of the
> integers in the container.
>
> What I have is:
>
> template<typename Sequence>
> struct sum
> {
> typedef typename mpl::iter_fold<
> Sequence,
> typename mpl::begin<Sequence>::type,
> mpl::plus< mpl::deref<_1>, mpl::deref<_2> >;
> // ^ what should this really be?
> > // what do i put after here?
> };
>
> Which obviously won't work. I don't know how to keep a running total as I
> iterate over the sequence with plus.
You are very close:
template<typename Sequence>
struct sum
{
typedef typename mpl::iter_fold<
mpl::iterator_range< // [begin + 1, end)
typename mpl::next<typename mpl::begin<Sequence>::type>::type
, typename mpl::end<Sequence>::type
>
, typename mpl::deref<typename mpl::begin<Sequence>::type>::type
, mpl::plus<mpl::deref<mpl::_2>, mpl::_1>
>::type type;
};
Note that there must be a special case for empty sequences.
-- Maxim Yegorushkin
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk