Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext
accumulate
Description

For a sequence Seq, initial state, and binary function object or function pointer f, accumulate repeatedly applies binary f to each element of Seq and the previous state.

Synopsis
template<
    typename Sequence,
    typename State,
    typename F
    >
typename result_of::accumulate<Sequence, State, F>::type accumulate(
    Sequence& seq, State const& initial_state, F const& f);

Table 1.34. Parameters

Parameter

Requirement

Description

seq

A model of Forward Sequence, f(eN ....f(e2,f(e1,initial_state))) must be a valid expression for each element e1 to eN in seq

Operation's argument

initial_state

Any type

Initial state

f

boost::result_of<F(E,S)>::type is the return type of f(e,s) for each element e of type E in seq, and current state s of type S

Operation's argument

Expression Semantics
accumulate(seq, initial_state, f);

Return type: Any type

Semantics: Equivalent to f(eN ....f(e2,f(e1,initial_state))) where e1 ...eN are the elements of seq.

Complexity

Linear, exactly result_of::size<Sequence>::value applications of f.

Header
#include <boost/fusion/algorithm/iteration/accumulate.hpp>
#include <boost/fusion/include/accumulate.hpp>
Example
struct make_string
{
    typedef std::string result_type;

    template<typename T>
    std::string operator()(const T& t, const std::string& str) const
    {
        return str + boost::lexical_cast<std::string>(t);
    }
};
...
const vector<int,int> vec(1,2);
assert(accumulate(vec,std::string(""), make_string()) == "12");

PrevUpHomeNext