1<?xml version="1.0" encoding="utf-8" ?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 4<head> 5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6<meta name="generator" content="Docutils 0.7: http://docutils.sourceforge.net/" /> 7<title>The MPL Reference Manual: for_each</title> 8<link rel="stylesheet" href="../style.css" type="text/css" /> 9</head> 10<body class="docframe refmanual"> 11<table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./runtime-algorithms.html" class="navigation-link">Prev</a> <a href="./metafunctions.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group">Back Along</span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./runtime-algorithms.html" class="navigation-link">Up</a> <a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td> 12<td class="header-group page-location"><a href="../refmanual.html" class="navigation-link">Front Page</a> / <a href="./algorithms.html" class="navigation-link">Algorithms</a> / <a href="./runtime-algorithms.html" class="navigation-link">Runtime Algorithms</a> / <a href="./for-each.html" class="navigation-link">for_each</a></td> 13</tr></table><div class="header-separator"></div> 14<div class="section" id="for-each"> 15<h1><a class="toc-backref" href="./runtime-algorithms.html#id1509">for_each</a></h1> 16<div class="section" id="id812"> 17<h3><a class="subsection-title" href="#synopsis" name="synopsis">Synopsis</a></h3> 18<pre class="literal-block"> 19template< 20 typename Sequence 21 , typename F 22 > 23void <a href="./for-each.html" class="identifier">for_each</a>( F f ); 24 25template< 26 typename Sequence 27 , typename TransformOp 28 , typename F 29 > 30void <a href="./for-each.html" class="identifier">for_each</a>( F f ); 31</pre> 32</div> 33<div class="section" id="id813"> 34<h3><a class="subsection-title" href="#description" name="description">Description</a></h3> 35<p><tt class="literal"><span class="pre"><a href="./for-each.html" class="identifier">for_each</a></span></tt> is a family of overloaded function templates:</p> 36<ul class="simple"> 37<li><tt class="literal"><span class="pre"><a href="./for-each.html" class="identifier">for_each</a><Sequence>(</span> <span class="pre">f</span> <span class="pre">)</span></tt> applies the runtime function object 38<tt class="literal"><span class="pre">f</span></tt> to every element in the [<tt class="literal"><span class="pre"><a href="./begin.html" class="identifier">begin</a><Sequence>::type</span></tt>, <tt class="literal"><span class="pre"><a href="./end.html" class="identifier">end</a><Sequence>::type</span></tt>) range.</li> 39<li><tt class="literal"><span class="pre"><a href="./for-each.html" class="identifier">for_each</a><Sequence,TransformOp>(</span> <span class="pre">f</span> <span class="pre">)</span></tt> applies the runtime function 40object <tt class="literal"><span class="pre">f</span></tt> to the result of the transformation <tt class="literal"><span class="pre">TransformOp</span></tt> of 41every element in the [<tt class="literal"><span class="pre"><a href="./begin.html" class="identifier">begin</a><Sequence>::type</span></tt>, <tt class="literal"><span class="pre"><a href="./end.html" class="identifier">end</a><Sequence>::type</span></tt>) range.</li> 42</ul> 43</div> 44<div class="section" id="id814"> 45<h3><a class="subsection-title" href="#header" name="header">Header</a></h3> 46<pre class="literal-block"> 47#include <<a href="../../../../boost/mpl/for_each.hpp" class="header">boost/mpl/for_each.hpp</a>> 48</pre> 49</div> 50<div class="section" id="id815"> 51<h3><a class="subsection-title" href="#parameters" name="parameters">Parameters</a></h3> 52<table border="1" class="docutils table"> 53<colgroup> 54<col width="21%" /> 55<col width="39%" /> 56<col width="39%" /> 57</colgroup> 58<thead valign="bottom"> 59<tr><th class="head">Parameter</th> 60<th class="head">Requirement</th> 61<th class="head">Description</th> 62</tr> 63</thead> 64<tbody valign="top"> 65<tr><td><tt class="literal"><span class="pre">Sequence</span></tt></td> 66<td><a class="reference internal" href="./forward-sequence.html">Forward Sequence</a></td> 67<td>A sequence to iterate.</td> 68</tr> 69<tr><td><tt class="literal"><span class="pre">TransformOp</span></tt></td> 70<td><a class="reference internal" href="./lambda-expression.html">Lambda Expression</a></td> 71<td>A transformation.</td> 72</tr> 73<tr><td><tt class="literal"><span class="pre">f</span></tt></td> 74<td>An <a class="reference external" href="https://boost.org/sgi/stl/UnaryFunction.html" target="_top">unary function object</a></td> 75<td>A runtime operation to apply.</td> 76</tr> 77</tbody> 78</table> 79</div> 80<div class="section" id="id816"> 81<h3><a class="subsection-title" href="#expression-semantics" name="expression-semantics">Expression semantics</a></h3> 82<p>For any <a class="reference internal" href="./forward-sequence.html">Forward Sequence</a> <tt class="literal"><span class="pre">s</span></tt>, <a class="reference internal" href="./lambda-expression.html">Lambda Expression</a> <tt class="literal"><span class="pre">op</span></tt> , and an 83<a class="reference external" href="https://boost.org/sgi/stl/UnaryFunction.html" target="_top">unary function object</a> <tt class="literal"><span class="pre">f</span></tt>:</p> 84<pre class="literal-block"> 85<a href="./for-each.html" class="identifier">for_each</a><s>( f ); 86</pre> 87<table class="docutils field-list" frame="void" rules="none"> 88<col class="field-name" /> 89<col class="field-body" /> 90<tbody valign="top"> 91<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first"><tt class="literal"><span class="pre">void</span></tt></p> 92</td> 93</tr> 94<tr class="field"><th class="field-name">Postcondition:</th><td class="field-body"><p class="first">Equivalent to</p> 95<pre class="literal-block"> 96typedef <a href="./begin.html" class="identifier">begin</a><Sequence>::type i<sub>1</sub>; 97<a class="reference external" href="http://www.boost.org/libs/utility/value_init.htm" target="_top">value_initialized</a>< <a href="./deref.html" class="identifier">deref</a><i<sub>1</sub>>::type > x<sub>1</sub>; 98f(boost::get(x<sub>1</sub>)); 99 100typedef <a href="./next.html" class="identifier">next</a><i<sub>1</sub>>::type i<sub>2</sub>; 101<a class="reference external" href="http://www.boost.org/libs/utility/value_init.htm" target="_top">value_initialized</a>< <a href="./deref.html" class="identifier">deref</a><i<sub>2</sub>>::type > x<sub>2</sub>; 102f(boost::get(x<sub>2</sub>)); 103<em>...</em> 104<a class="reference external" href="http://www.boost.org/libs/utility/value_init.htm" target="_top">value_initialized</a>< <a href="./deref.html" class="identifier">deref</a><i<sub>n</sub>>::type > x<sub>n</sub>; 105f(boost::get(x<sub>n</sub>)); 106typedef <a href="./next.html" class="identifier">next</a><i<sub>n</sub>>::type last; 107</pre> 108<p class="last">where <tt class="literal"><span class="pre">n</span> <span class="pre">==</span> <span class="pre"><a href="./size.html" class="identifier">size</a><s>::value</span></tt> and <tt class="literal"><span class="pre">last</span></tt> is identical to 109<tt class="literal"><span class="pre"><a href="./end.html" class="identifier">end</a><s>::type</span></tt>; no effect if <tt class="literal"><span class="pre"><a href="./empty.html" class="identifier">empty</a><s>::value</span> <span class="pre">==</span> <span class="pre">true</span></tt>.</p> 110</td> 111</tr> 112</tbody> 113</table> 114<pre class="literal-block"> 115<a href="./for-each.html" class="identifier">for_each</a><s,op>( f ); 116</pre> 117<table class="docutils field-list" frame="void" rules="none"> 118<col class="field-name" /> 119<col class="field-body" /> 120<tbody valign="top"> 121<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first"><tt class="literal"><span class="pre">void</span></tt></p> 122</td> 123</tr> 124<tr class="field"><th class="field-name">Postcondition:</th><td class="field-body"><p class="first">Equivalent to</p> 125<pre class="last literal-block"> 126<a href="./for-each.html" class="identifier">for_each</a>< <a href="./transform-view.html" class="identifier">transform_view</a><s,op> >( f ); 127</pre> 128</td> 129</tr> 130</tbody> 131</table> 132</div> 133<div class="section" id="id817"> 134<h3><a class="subsection-title" href="#complexity" name="complexity">Complexity</a></h3> 135<p>Linear. Exactly <tt class="literal"><span class="pre"><a href="./size.html" class="identifier">size</a><s>::value</span></tt> applications of <tt class="literal"><span class="pre">op</span></tt> and <tt class="literal"><span class="pre">f</span></tt>.</p> 136</div> 137<div class="section" id="id818"> 138<h3><a class="subsection-title" href="#example" name="example">Example</a></h3> 139<pre class="literal-block"> 140struct value_printer 141{ 142 template< typename U > void operator()(U x) 143 { 144 std::cout << x << '\n'; 145 } 146}; 147 148int main() 149{ 150 <a href="./for-each.html" class="identifier">for_each</a>< <a href="./range-c.html" class="identifier">range_c</a><int,0,10> >( value_printer() ); 151} 152</pre> 153</div> 154<div class="section" id="id819"> 155<h3><a class="subsection-title" href="#see-also" name="see-also">See also</a></h3> 156<p><a class="reference internal" href="./runtime-algorithms.html">Runtime Algorithms</a>, <a class="reference internal" href="./views.html">Views</a>, <a class="reference internal" href="./transform-view.html">transform_view</a></p> 157</div> 158</div> 159 160<div class="footer-separator"></div> 161<table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./runtime-algorithms.html" class="navigation-link">Prev</a> <a href="./metafunctions.html" class="navigation-link">Next</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group">Back Along</span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./runtime-algorithms.html" class="navigation-link">Up</a> <a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator"> | </span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td> 162<td><div class="copyright-footer"><div class="copyright">Copyright © 2001-2009 Aleksey Gurtovoy and David Abrahams</div> 163Distributed under the Boost Software License, Version 1.0. (See accompanying 164file LICENSE_1_0.txt or copy at <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)</div></td></tr></table></body> 165</html> 166