• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. Algorithms/Runtime Algorithms//for_each |10
2
3for_each
4========
5
6Synopsis
7--------
8
9.. parsed-literal::
10
11    template<
12          typename Sequence
13        , typename F
14        >
15    void for_each( F f );
16
17    template<
18          typename Sequence
19        , typename TransformOp
20        , typename F
21        >
22    void for_each( F f );
23
24
25Description
26-----------
27
28``for_each`` is a family of overloaded function templates:
29
30* ``for_each<Sequence>( f )`` applies the runtime function object
31  ``f`` to every element in the |begin/end<Sequence>| range.
32
33* ``for_each<Sequence,TransformOp>( f )`` applies the runtime function
34  object ``f`` to the result of the transformation ``TransformOp`` of
35  every element in the |begin/end<Sequence>| range.
36
37
38Header
39------
40
41.. parsed-literal::
42
43    #include <boost/mpl/for_each.hpp>
44
45
46Parameters
47----------
48
49+-------------------+-----------------------------------+-----------------------------------+
50| Parameter         | Requirement                       | Description                       |
51+===================+===================================+===================================+
52| ``Sequence``      | |Forward Sequence|                | A sequence to iterate.            |
53+-------------------+-----------------------------------+-----------------------------------+
54| ``TransformOp``   | |Lambda Expression|               | A transformation.                 |
55+-------------------+-----------------------------------+-----------------------------------+
56| ``f``             | An |unary function object|        | A runtime operation to apply.     |
57+-------------------+-----------------------------------+-----------------------------------+
58
59
60Expression semantics
61--------------------
62
63For any |Forward Sequence| ``s``, |Lambda Expression| ``op`` , and an
64|unary function object| ``f``:
65
66.. parsed-literal::
67
68    for_each<s>( f );
69
70:Return type:
71    ``void``
72
73:Postcondition:
74    Equivalent to
75
76    .. parsed-literal::
77
78        typedef begin<Sequence>::type i\ :sub:`1`;
79        |value_initialized|\ < deref<i\ :sub:`1`>::type > x\ :sub:`1`;
80        f(boost::get(x\ :sub:`1`));
81
82        typedef next<i\ :sub:`1`>::type i\ :sub:`2`;
83        |value_initialized|\ < deref<i\ :sub:`2`>::type > x\ :sub:`2`;
84        f(boost::get(x\ :sub:`2`));
85        |...|
86        |value_initialized|\ < deref<i\ :sub:`n`>::type > x\ :sub:`n`;
87        f(boost::get(x\ :sub:`n`));
88        typedef next<i\ :sub:`n`>::type last;
89
90    where ``n == size<s>::value`` and ``last`` is identical to
91    ``end<s>::type``; no effect if ``empty<s>::value == true``.
92
93
94.. parsed-literal::
95
96    for_each<s,op>( f );
97
98:Return type:
99    ``void``
100
101:Postcondition:
102    Equivalent to
103
104    .. parsed-literal::
105
106        for_each< transform_view<s,op> >( f );
107
108
109Complexity
110----------
111
112Linear. Exactly ``size<s>::value`` applications of ``op`` and ``f``.
113
114
115Example
116-------
117
118.. parsed-literal::
119
120    struct value_printer
121    {
122        template< typename U > void operator()(U x)
123        {
124            std::cout << x << '\\n';
125        }
126    };
127
128    int main()
129    {
130        for_each< range_c<int,0,10> >( value_printer() );
131    }
132
133
134See also
135--------
136
137|Runtime Algorithms|, |Views|, |transform_view|
138
139.. |unary function object| replace:: `unary function object <https://boost.org/sgi/stl/UnaryFunction.html>`__
140.. |value_initialized| replace:: `value_initialized <http://www.boost.org/libs/utility/value_init.htm>`__
141
142
143.. copyright:: Copyright �  2001-2009 Aleksey Gurtovoy and David Abrahams
144   Distributed under the Boost Software License, Version 1.0. (See accompanying
145   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
146