for-each.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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>&nbsp;<a href="./metafunctions.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./runtime-algorithms.html" class="navigation-link">Up</a>&nbsp;<a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</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">
  19. template&lt;
  20. typename Sequence
  21. , typename F
  22. &gt;
  23. void <a href="./for-each.html" class="identifier">for_each</a>( F f );
  24. template&lt;
  25. typename Sequence
  26. , typename TransformOp
  27. , typename F
  28. &gt;
  29. void <a href="./for-each.html" class="identifier">for_each</a>( F f );
  30. </pre>
  31. </div>
  32. <div class="section" id="id813">
  33. <h3><a class="subsection-title" href="#description" name="description">Description</a></h3>
  34. <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>
  35. <ul class="simple">
  36. <li><tt class="literal"><span class="pre"><a href="./for-each.html" class="identifier">for_each</a>&lt;Sequence&gt;(</span> <span class="pre">f</span> <span class="pre">)</span></tt> applies the runtime function object
  37. <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>&lt;Sequence&gt;::type</span></tt>, <tt class="literal"><span class="pre"><a href="./end.html" class="identifier">end</a>&lt;Sequence&gt;::type</span></tt>) range.</li>
  38. <li><tt class="literal"><span class="pre"><a href="./for-each.html" class="identifier">for_each</a>&lt;Sequence,TransformOp&gt;(</span> <span class="pre">f</span> <span class="pre">)</span></tt> applies the runtime function
  39. object <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
  40. every element in the [<tt class="literal"><span class="pre"><a href="./begin.html" class="identifier">begin</a>&lt;Sequence&gt;::type</span></tt>, <tt class="literal"><span class="pre"><a href="./end.html" class="identifier">end</a>&lt;Sequence&gt;::type</span></tt>) range.</li>
  41. </ul>
  42. </div>
  43. <div class="section" id="id814">
  44. <h3><a class="subsection-title" href="#header" name="header">Header</a></h3>
  45. <pre class="literal-block">
  46. #include &lt;<a href="../../../../boost/mpl/for_each.hpp" class="header">boost/mpl/for_each.hpp</a>&gt;
  47. </pre>
  48. </div>
  49. <div class="section" id="id815">
  50. <h3><a class="subsection-title" href="#parameters" name="parameters">Parameters</a></h3>
  51. <table border="1" class="docutils table">
  52. <colgroup>
  53. <col width="21%" />
  54. <col width="39%" />
  55. <col width="39%" />
  56. </colgroup>
  57. <thead valign="bottom">
  58. <tr><th class="head">Parameter</th>
  59. <th class="head">Requirement</th>
  60. <th class="head">Description</th>
  61. </tr>
  62. </thead>
  63. <tbody valign="top">
  64. <tr><td><tt class="literal"><span class="pre">Sequence</span></tt></td>
  65. <td><a class="reference internal" href="./forward-sequence.html">Forward Sequence</a></td>
  66. <td>A sequence to iterate.</td>
  67. </tr>
  68. <tr><td><tt class="literal"><span class="pre">TransformOp</span></tt></td>
  69. <td><a class="reference internal" href="./lambda-expression.html">Lambda Expression</a></td>
  70. <td>A transformation.</td>
  71. </tr>
  72. <tr><td><tt class="literal"><span class="pre">f</span></tt></td>
  73. <td>An <a class="reference external" href="https://boost.org/sgi/stl/UnaryFunction.html" target="_top">unary function object</a></td>
  74. <td>A runtime operation to apply.</td>
  75. </tr>
  76. </tbody>
  77. </table>
  78. </div>
  79. <div class="section" id="id816">
  80. <h3><a class="subsection-title" href="#expression-semantics" name="expression-semantics">Expression semantics</a></h3>
  81. <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
  82. <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>
  83. <pre class="literal-block">
  84. <a href="./for-each.html" class="identifier">for_each</a>&lt;s&gt;( f );
  85. </pre>
  86. <table class="docutils field-list" frame="void" rules="none">
  87. <col class="field-name" />
  88. <col class="field-body" />
  89. <tbody valign="top">
  90. <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>
  91. </td>
  92. </tr>
  93. <tr class="field"><th class="field-name">Postcondition:</th><td class="field-body"><p class="first">Equivalent to</p>
  94. <pre class="literal-block">
  95. typedef <a href="./begin.html" class="identifier">begin</a>&lt;Sequence&gt;::type i<sub>1</sub>;
  96. <a class="reference external" href="http://www.boost.org/libs/utility/value_init.htm" target="_top">value_initialized</a>&lt; <a href="./deref.html" class="identifier">deref</a>&lt;i<sub>1</sub>&gt;::type &gt; x<sub>1</sub>;
  97. f(boost::get(x<sub>1</sub>));
  98. typedef <a href="./next.html" class="identifier">next</a>&lt;i<sub>1</sub>&gt;::type i<sub>2</sub>;
  99. <a class="reference external" href="http://www.boost.org/libs/utility/value_init.htm" target="_top">value_initialized</a>&lt; <a href="./deref.html" class="identifier">deref</a>&lt;i<sub>2</sub>&gt;::type &gt; x<sub>2</sub>;
  100. f(boost::get(x<sub>2</sub>));
  101. <em>...</em>
  102. <a class="reference external" href="http://www.boost.org/libs/utility/value_init.htm" target="_top">value_initialized</a>&lt; <a href="./deref.html" class="identifier">deref</a>&lt;i<sub>n</sub>&gt;::type &gt; x<sub>n</sub>;
  103. f(boost::get(x<sub>n</sub>));
  104. typedef <a href="./next.html" class="identifier">next</a>&lt;i<sub>n</sub>&gt;::type last;
  105. </pre>
  106. <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>&lt;s&gt;::value</span></tt> and <tt class="literal"><span class="pre">last</span></tt> is identical to
  107. <tt class="literal"><span class="pre"><a href="./end.html" class="identifier">end</a>&lt;s&gt;::type</span></tt>; no effect if <tt class="literal"><span class="pre"><a href="./empty.html" class="identifier">empty</a>&lt;s&gt;::value</span> <span class="pre">==</span> <span class="pre">true</span></tt>.</p>
  108. </td>
  109. </tr>
  110. </tbody>
  111. </table>
  112. <pre class="literal-block">
  113. <a href="./for-each.html" class="identifier">for_each</a>&lt;s,op&gt;( f );
  114. </pre>
  115. <table class="docutils field-list" frame="void" rules="none">
  116. <col class="field-name" />
  117. <col class="field-body" />
  118. <tbody valign="top">
  119. <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>
  120. </td>
  121. </tr>
  122. <tr class="field"><th class="field-name">Postcondition:</th><td class="field-body"><p class="first">Equivalent to</p>
  123. <pre class="last literal-block">
  124. <a href="./for-each.html" class="identifier">for_each</a>&lt; <a href="./transform-view.html" class="identifier">transform_view</a>&lt;s,op&gt; &gt;( f );
  125. </pre>
  126. </td>
  127. </tr>
  128. </tbody>
  129. </table>
  130. </div>
  131. <div class="section" id="id817">
  132. <h3><a class="subsection-title" href="#complexity" name="complexity">Complexity</a></h3>
  133. <p>Linear. Exactly <tt class="literal"><span class="pre"><a href="./size.html" class="identifier">size</a>&lt;s&gt;::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>
  134. </div>
  135. <div class="section" id="id818">
  136. <h3><a class="subsection-title" href="#example" name="example">Example</a></h3>
  137. <pre class="literal-block">
  138. struct value_printer
  139. {
  140. template&lt; typename U &gt; void operator()(U x)
  141. {
  142. std::cout &lt;&lt; x &lt;&lt; '\n';
  143. }
  144. };
  145. int main()
  146. {
  147. <a href="./for-each.html" class="identifier">for_each</a>&lt; <a href="./range-c.html" class="identifier">range_c</a>&lt;int,0,10&gt; &gt;( value_printer() );
  148. }
  149. </pre>
  150. </div>
  151. <div class="section" id="id819">
  152. <h3><a class="subsection-title" href="#see-also" name="see-also">See also</a></h3>
  153. <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>
  154. </div>
  155. </div>
  156. <div class="footer-separator"></div>
  157. <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>&nbsp;<a href="./metafunctions.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./runtime-algorithms.html" class="navigation-link">Up</a>&nbsp;<a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td>
  158. <td><div class="copyright-footer"><div class="copyright">Copyright © 2001-2009 Aleksey Gurtovoy and David Abrahams</div>
  159. Distributed under the Boost Software License, Version 1.0. (See accompanying
  160. file 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>
  161. </html>