.. Algorithms/Runtime Algorithms//for_each |10 for_each ======== Synopsis -------- .. parsed-literal:: template< typename Sequence , typename F > void for_each( F f ); template< typename Sequence , typename TransformOp , typename F > void for_each( F f ); Description ----------- ``for_each`` is a family of overloaded function templates: * ``for_each( f )`` applies the runtime function object ``f`` to every element in the |begin/end| range. * ``for_each( f )`` applies the runtime function object ``f`` to the result of the transformation ``TransformOp`` of every element in the |begin/end| range. Header ------ .. parsed-literal:: #include Parameters ---------- +-------------------+-----------------------------------+-----------------------------------+ | Parameter | Requirement | Description | +===================+===================================+===================================+ | ``Sequence`` | |Forward Sequence| | A sequence to iterate. | +-------------------+-----------------------------------+-----------------------------------+ | ``TransformOp`` | |Lambda Expression| | A transformation. | +-------------------+-----------------------------------+-----------------------------------+ | ``f`` | An |unary function object| | A runtime operation to apply. | +-------------------+-----------------------------------+-----------------------------------+ Expression semantics -------------------- For any |Forward Sequence| ``s``, |Lambda Expression| ``op`` , and an |unary function object| ``f``: .. parsed-literal:: for_each( f ); :Return type: ``void`` :Postcondition: Equivalent to .. parsed-literal:: typedef begin::type i\ :sub:`1`; |value_initialized|\ < deref::type > x\ :sub:`1`; f(boost::get(x\ :sub:`1`)); typedef next::type i\ :sub:`2`; |value_initialized|\ < deref::type > x\ :sub:`2`; f(boost::get(x\ :sub:`2`)); |...| |value_initialized|\ < deref::type > x\ :sub:`n`; f(boost::get(x\ :sub:`n`)); typedef next::type last; where ``n == size::value`` and ``last`` is identical to ``end::type``; no effect if ``empty::value == true``. .. parsed-literal:: for_each( f ); :Return type: ``void`` :Postcondition: Equivalent to .. parsed-literal:: for_each< transform_view >( f ); Complexity ---------- Linear. Exactly ``size::value`` applications of ``op`` and ``f``. Example ------- .. parsed-literal:: struct value_printer { template< typename U > void operator()(U x) { std::cout << x << '\\n'; } }; int main() { for_each< range_c >( value_printer() ); } See also -------- |Runtime Algorithms|, |Views|, |transform_view| .. |unary function object| replace:: `unary function object `__ .. |value_initialized| replace:: `value_initialized `__ .. copyright:: Copyright © 2001-2009 Aleksey Gurtovoy and David Abrahams Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)