1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>for_each</title> 5<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../../index.html" title="Chapter 1. Range 2.0"> 8<link rel="up" href="../new.html" title="New algorithms"> 9<link rel="prev" href="erase.html" title="erase"> 10<link rel="next" href="insert.html" title="insert"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="erase.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../new.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="insert.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h5 class="title"> 27<a name="range.reference.algorithms.new.for_each"></a><a class="link" href="for_each.html" title="for_each">for_each</a> 28</h5></div></div></div> 29<h6> 30<a name="range.reference.algorithms.new.for_each.h0"></a> 31 <span class="phrase"><a name="range.reference.algorithms.new.for_each.prototype"></a></span><a class="link" href="for_each.html#range.reference.algorithms.new.for_each.prototype">Prototype</a> 32 </h6> 33<p> 34</p> 35<pre class="programlisting"><span class="keyword">template</span><span class="special"><</span> 36 <span class="keyword">class</span> <span class="identifier">SinglePassRange1</span><span class="special">,</span> 37 <span class="keyword">class</span> <span class="identifier">SinglePassRange2</span><span class="special">,</span> 38 <span class="keyword">class</span> <span class="identifier">BinaryFunction</span> 39 <span class="special">></span> 40<span class="identifier">BinaryFunction</span> <span class="identifier">for_each</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">SinglePassRange1</span><span class="special">&</span> <span class="identifier">rng1</span><span class="special">,</span> 41 <span class="keyword">const</span> <span class="identifier">SinglePassRange2</span><span class="special">&</span> <span class="identifier">rng2</span><span class="special">,</span> 42 <span class="identifier">BinaryFunction</span> <span class="identifier">fn</span><span class="special">);</span> 43 44<span class="keyword">template</span><span class="special"><</span> 45 <span class="keyword">class</span> <span class="identifier">SinglePassRange1</span><span class="special">,</span> 46 <span class="keyword">class</span> <span class="identifier">SinglePassRange2</span><span class="special">,</span> 47 <span class="keyword">class</span> <span class="identifier">BinaryFunction</span> 48 <span class="special">></span> 49<span class="identifier">BinaryFunction</span> <span class="identifier">for_each</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">SinglePassRange1</span><span class="special">&</span> <span class="identifier">rng1</span><span class="special">,</span> 50 <span class="identifier">SinglePassRange2</span><span class="special">&</span> <span class="identifier">rng2</span><span class="special">,</span> 51 <span class="identifier">BinaryFunction</span> <span class="identifier">fn</span><span class="special">);</span> 52 53<span class="keyword">template</span><span class="special"><</span> 54 <span class="keyword">class</span> <span class="identifier">SinglePassRange1</span><span class="special">,</span> 55 <span class="keyword">class</span> <span class="identifier">SinglePassRange2</span><span class="special">,</span> 56 <span class="keyword">class</span> <span class="identifier">BinaryFunction</span> 57 <span class="special">></span> 58<span class="identifier">BinaryFunction</span> <span class="identifier">for_each</span><span class="special">(</span><span class="identifier">SinglePassRange1</span><span class="special">&</span> <span class="identifier">rng1</span><span class="special">,</span> 59 <span class="keyword">const</span> <span class="identifier">SinglePassRange2</span><span class="special">&</span> <span class="identifier">rng2</span><span class="special">,</span> 60 <span class="identifier">BinaryFunction</span> <span class="identifier">fn</span><span class="special">);</span> 61 62<span class="keyword">template</span><span class="special"><</span> 63 <span class="keyword">class</span> <span class="identifier">SinglePassRange1</span><span class="special">,</span> 64 <span class="keyword">class</span> <span class="identifier">SinglePassRange2</span><span class="special">,</span> 65 <span class="keyword">class</span> <span class="identifier">BinaryFunction</span> 66 <span class="special">></span> 67<span class="identifier">BinaryFunction</span> <span class="identifier">for_each</span><span class="special">(</span><span class="identifier">SinglePassRange1</span><span class="special">&</span> <span class="identifier">rng1</span><span class="special">,</span> 68 <span class="identifier">SinglePassRange2</span><span class="special">&</span> <span class="identifier">rng2</span><span class="special">,</span> 69 <span class="identifier">BinaryFunction</span> <span class="identifier">fn</span><span class="special">);</span> 70</pre> 71<p> 72 </p> 73<h6> 74<a name="range.reference.algorithms.new.for_each.h1"></a> 75 <span class="phrase"><a name="range.reference.algorithms.new.for_each.description"></a></span><a class="link" href="for_each.html#range.reference.algorithms.new.for_each.description">Description</a> 76 </h6> 77<p> 78 <code class="computeroutput"><span class="identifier">for_each</span></code> traverses forward 79 through <code class="computeroutput"><span class="identifier">rng1</span></code> and <code class="computeroutput"><span class="identifier">rng2</span></code> simultaneously. For each iteration, 80 the element <code class="computeroutput"><span class="identifier">x</span></code> is used 81 from <code class="computeroutput"><span class="identifier">rng1</span></code> and the corresponding 82 element <code class="computeroutput"><span class="identifier">y</span></code> is used from 83 <code class="computeroutput"><span class="identifier">rng2</span></code> to invoke <code class="computeroutput"><span class="identifier">fn</span><span class="special">(</span><span class="identifier">x</span><span class="special">,</span><span class="identifier">y</span><span class="special">)</span></code>. 84 </p> 85<p> 86 Iteration is stopped upon reaching the end of the shorter of <code class="computeroutput"><span class="identifier">rng1</span></code>, or <code class="computeroutput"><span class="identifier">rng2</span></code>. 87 It is safe to call this function with unequal length ranges. 88 </p> 89<h6> 90<a name="range.reference.algorithms.new.for_each.h2"></a> 91 <span class="phrase"><a name="range.reference.algorithms.new.for_each.definition"></a></span><a class="link" href="for_each.html#range.reference.algorithms.new.for_each.definition">Definition</a> 92 </h6> 93<p> 94 Defined in the header file <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">range</span><span class="special">/</span><span class="identifier">algorithm_ext</span><span class="special">/</span><span class="identifier">for_each</span><span class="special">.</span><span class="identifier">hpp</span></code> 95 </p> 96<h6> 97<a name="range.reference.algorithms.new.for_each.h3"></a> 98 <span class="phrase"><a name="range.reference.algorithms.new.for_each.requirements"></a></span><a class="link" href="for_each.html#range.reference.algorithms.new.for_each.requirements">Requirements</a> 99 </h6> 100<div class="orderedlist"><ol class="orderedlist" type="1"> 101<li class="listitem"> 102 <code class="computeroutput"><span class="identifier">SinglePassRange1</span></code> 103 is a model of the <a class="link" href="../../../concepts/single_pass_range.html" title="Single Pass Range">Single 104 Pass Range</a> Concept. 105 </li> 106<li class="listitem"> 107 <code class="computeroutput"><span class="identifier">SinglePassRange2</span></code> 108 is a model of the <a class="link" href="../../../concepts/single_pass_range.html" title="Single Pass Range">Single 109 Pass Range</a> Concept. 110 </li> 111<li class="listitem"> 112 <code class="computeroutput"><span class="identifier">BinaryFunction</span></code> is 113 a model of the <code class="computeroutput"><span class="identifier">BinaryFunctionConcept</span></code>. 114 </li> 115<li class="listitem"> 116 <code class="computeroutput"><span class="identifier">SinglePassRange1</span></code>'s 117 value type is convertible to <code class="computeroutput"><span class="identifier">BinaryFunction</span></code>'s 118 first argument type. 119 </li> 120<li class="listitem"> 121 <code class="computeroutput"><span class="identifier">SinglepassRange2</span></code>'s 122 value type is convertible to <code class="computeroutput"><span class="identifier">BinaryFunction</span></code>'s 123 second argument type. 124 </li> 125</ol></div> 126<h6> 127<a name="range.reference.algorithms.new.for_each.h4"></a> 128 <span class="phrase"><a name="range.reference.algorithms.new.for_each.complexity"></a></span><a class="link" href="for_each.html#range.reference.algorithms.new.for_each.complexity">Complexity</a> 129 </h6> 130<p> 131 Linear. Exactly <code class="computeroutput"><span class="identifier">min</span><span class="special">(</span><span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng1</span><span class="special">),</span> <span class="identifier">distance</span><span class="special">(</span><span class="identifier">rng2</span><span class="special">))</span></code> 132 applications of <code class="computeroutput"><span class="identifier">BinaryFunction</span></code>. 133 </p> 134</div> 135<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 136<td align="left"></td> 137<td align="right"><div class="copyright-footer">Copyright © 2003-2010 Thorsten Ottosen, 138 Neil Groves<p> 139 Distributed under the Boost Software License, Version 1.0. (See accompanying 140 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 141 </p> 142</div></td> 143</tr></table> 144<hr> 145<div class="spirit-nav"> 146<a accesskey="p" href="erase.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../new.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="insert.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> 147</div> 148</body> 149</html> 150