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 initial_state, and binary function object or function pointer f, accumulate returns the result of the repeated application of binary f to the result of the previous f invocation (inital_state if it is the first call) and each element of seq.

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

Table 1.41. Parameters

Parameter

Requirement

Description

seq

A model of Forward Sequence

Operation's argument

initial_state

Any type

Initial state

f

f(s,e) with return type boost::result_of<F(S,E)>::type must be a valid expression for current state s of type S, and for each element e of type E in seq

Operation's argument


Expression Semantics
accumulate(seq, initial_state, f);

Return type: Any type

Semantics: Equivalent to f(... f(f(initial_state,e1),e2) ...eN) where e1 ...eN are the consecutive 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 std::string& str, const T& t) 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