...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
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.
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 |
---|---|---|
|
A model of Forward
Sequence, |
Operation's argument |
|
Any type |
Initial state |
|
|
Operation's argument |
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
.
Linear, exactly
applications of result_of::size
<Sequence>::valuef
.
#include <boost/fusion/algorithm/iteration/accumulate.hpp> #include <boost/fusion/include/accumulate.hpp>
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); } }; ... constvector
<int,int> vec(1,2); assert(accumulate
(vec,std::string(""), make_string()) == "12");