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

PrevUpHomeNext
for_each
Description

Applies a unary function object to each element of a sequence.

Synopsis
template<
    typename Sequence,
    typename F
    >
typename result_of::for_each<Sequence, F>::type for_each(
    Sequence& seq, F f);

Table 1.45. Parameters

Parameter

Requirement

Description

seq

A model of Forward Sequence, f(e) must be a valid expression for each element e in seq

Operation's argument

f

A unary Regular Callable Object

Operation's argument


Expression Semantics
for_each(seq, f);

Return type: void

Semantics: Calls f(e) for each element e in seq.

Complexity

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

Header
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/include/for_each.hpp>
Example
struct increment
{
    template<typename T>
    void operator()(T& t) const
    {
        ++t;
    }
};
...
vector<int,int> vec(1,2);
for_each(vec, increment());
assert(vec == make_vector(2,3));

PrevUpHomeNext