blob: ec59ed00531f851680d25e438630bb828bb44856 [file] [log] [blame]
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed
with this work for additional information regarding copyright
ownership. The ASF licenses this file to you under the Apache
License, Version 2.0 (the License); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
Copyright 1999-2007 Rogue Wave Software, Inc.
-->
<HTML>
<HEAD>
<TITLE>The for_each() Algorithm</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="13-7.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="14.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library User's Guide</B></DIV>
<H2>13.8 The for_each() Algorithm</H2>
<A NAME="idx343"><!></A>
<P>The algorithm <SAMP>std::for_each()</SAMP> applies a function to all elements in a collection. This algorithm takes three arguments: the first two provide the <B><I><A HREF="../stdlibref/iterator.html">iterator</A></I></B>s that describe the sequence to be evaluated, and the third is a one-argument function. The algorithm <SAMP>std::for_each()</SAMP> applies the function to each value of the sequence, passing the value as an argument:</P>
<UL><PRE>
namespace std {
Function for_each(InputIterator first,
InputIterator last, Function);
}
</PRE></UL>
<P>For example, the following code fragment, which uses the <SAMP>print_if_leap()</SAMP> function, prints a list of the leap years that occur between 1900 and 1997: </P>
<UL><PRE>
std::cout &lt;&lt; "leap years between 1990 and 1997 are: ";
std::for_each(1990, 1997, print_if_leap);
std::cout &lt;&lt; std::endl;
</PRE></UL>
<P>The argument function is guaranteed to be invoked only once for each element in the sequence. The <SAMP>std::for_each()</SAMP> algorithm itself returns the value of the third argument, although this is usually ignored.</P>
<BLOCKQUOTE><HR><B>
NOTE -- The function passed as the third argument is not permitted to make any modifications to the sequence, so it can only achieve a result by means of a side effect, such as printing, assigning a value to a global or static variable, or invoking another function that produces a side effect. If the argument function returns any result, it is ignored.
</B><HR></BLOCKQUOTE>
<P>The following example searches an array of <SAMP>int</SAMP> values representing dates, to determine which vintage wine years were also leap years:</P>
<UL><PRE>
int vintageYears[] = {1947, 1955, 1960, 1967, 1994};
...
std::cout &lt;&lt; "vintage years which were also leap years are: ";
std::for_each(vintageYears, vintageYears + 5, print_if_leap);
std::cout &lt;&lt; std::endl;
</PRE></UL>
<P>Side effects need not be restricted to printing. Assume we have a function <SAMP>countCaps()</SAMP> that counts the occurrence of capital letters:</P>
<UL><PRE>
int capCount = 0;
void countCaps(char c) { if (std::isupper(c)) capCount++; }
</PRE></UL>
<P>The following example counts the number of capital letters in a string value:</P>
<UL><PRE>
std::string advice = "Never Trust Anybody Over 30!";
std::for_each(advice.begin(), advice.end(),countCaps);
std::cout &lt;&lt; "upper-case letter count is "
&lt;&lt; capCount &lt;&lt; std::endl;
</PRE></UL>
<BR>
<HR>
<A HREF="13-7.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="14.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>