1*404b540aSrobert<?xml version="1.0" encoding="ISO-8859-1"?> 2*404b540aSrobert<!DOCTYPE html 3*404b540aSrobert PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 4*404b540aSrobert "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5*404b540aSrobert 6*404b540aSrobert<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 7*404b540aSrobert<head> 8*404b540aSrobert <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 9*404b540aSrobert <meta name="AUTHOR" content="pme@gcc.gnu.org (Phil Edwards)" /> 10*404b540aSrobert <meta name="KEYWORDS" content="HOWTO, libstdc++, GCC, g++, libg++, STL" /> 11*404b540aSrobert <meta name="DESCRIPTION" content="HOWTO for the libstdc++ chapter 25." /> 12*404b540aSrobert <meta name="GENERATOR" content="vi and eight fingers" /> 13*404b540aSrobert <title>libstdc++-v3 HOWTO: Chapter 25: Algorithms</title> 14*404b540aSrobert<link rel="StyleSheet" href="../lib3styles.css" type="text/css" /> 15*404b540aSrobert<link rel="Start" href="../documentation.html" type="text/html" 16*404b540aSrobert title="GNU C++ Standard Library" /> 17*404b540aSrobert<link rel="Prev" href="../24_iterators/howto.html" type="text/html" 18*404b540aSrobert title="Iterators" /> 19*404b540aSrobert<link rel="Next" href="../26_numerics/howto.html" type="text/html" 20*404b540aSrobert title="Numerics" /> 21*404b540aSrobert<link rel="Copyright" href="../17_intro/license.html" type="text/html" /> 22*404b540aSrobert<link rel="Help" href="../faq/index.html" type="text/html" title="F.A.Q." /> 23*404b540aSrobert</head> 24*404b540aSrobert<body> 25*404b540aSrobert 26*404b540aSrobert<h1 class="centered"><a name="top">Chapter 25: Algorithms</a></h1> 27*404b540aSrobert 28*404b540aSrobert<p>Chapter 25 deals with the generalized subroutines for automatically 29*404b540aSrobert transforming lemmings into gold. 30*404b540aSrobert</p> 31*404b540aSrobert 32*404b540aSrobert 33*404b540aSrobert<!-- ####################################################### --> 34*404b540aSrobert<hr /> 35*404b540aSrobert<h1>Contents</h1> 36*404b540aSrobert<ul> 37*404b540aSrobert <li><a href="#1">Prerequisites</a></li> 38*404b540aSrobert <li><a href="#2">Special <code>swap</code>s</a></li> 39*404b540aSrobert</ul> 40*404b540aSrobert 41*404b540aSrobert<hr /> 42*404b540aSrobert 43*404b540aSrobert<!-- ####################################################### --> 44*404b540aSrobert 45*404b540aSrobert<h2><a name="1">Prerequisites</a></h2> 46*404b540aSrobert <p>The neatest accomplishment of the algorithms chapter is that all the 47*404b540aSrobert work is done via iterators, not containers directly. This means two 48*404b540aSrobert important things: 49*404b540aSrobert </p> 50*404b540aSrobert <ol> 51*404b540aSrobert <li>Anything that behaves like an iterator can be used in one of 52*404b540aSrobert these algorithms. Raw pointers make great candidates, thus 53*404b540aSrobert built-in arrays are fine containers, as well as your own iterators. 54*404b540aSrobert </li> 55*404b540aSrobert <li>The algorithms do not (and cannot) affect the container as a 56*404b540aSrobert whole; only the things between the two iterator endpoints. If 57*404b540aSrobert you pass a range of iterators only enclosing the middle third of 58*404b540aSrobert a container, then anything outside that range is inviolate. 59*404b540aSrobert </li> 60*404b540aSrobert </ol> 61*404b540aSrobert <p>Even strings can be fed through the algorithms here, although the 62*404b540aSrobert string class has specialized versions of many of these functions (for 63*404b540aSrobert example, <code>string::find()</code>). Most of the examples on this 64*404b540aSrobert page will use simple arrays of integers as a playground for 65*404b540aSrobert algorithms, just to keep things simple. 66*404b540aSrobert <a name="Nsize">The use of <strong>N</strong></a> as a size in the 67*404b540aSrobert examples is to keep things easy to read but probably won't be valid 68*404b540aSrobert code. You can use wrappers such as those described in the 69*404b540aSrobert <a href="../23_containers/howto.html">containers chapter</a> to keep 70*404b540aSrobert real code readable. 71*404b540aSrobert </p> 72*404b540aSrobert <p>The single thing that trips people up the most is the definition of 73*404b540aSrobert <em>range</em> used with iterators; the famous 74*404b540aSrobert "past-the-end" rule that everybody loves to hate. The 75*404b540aSrobert <a href="../24_iterators/howto.html#2">iterators chapter</a> of this 76*404b540aSrobert document has a complete explanation of this simple rule that seems to 77*404b540aSrobert cause so much confusion. Once you get <em>range</em> into your head 78*404b540aSrobert (it's not that hard, honest!), then the algorithms are a cakewalk. 79*404b540aSrobert </p> 80*404b540aSrobert <p>Return <a href="#top">to top of page</a> or 81*404b540aSrobert <a href="../faq/index.html">to the FAQ</a>. 82*404b540aSrobert </p> 83*404b540aSrobert 84*404b540aSrobert<hr /> 85*404b540aSrobert<h2><a name="2">Special <code>swap</code>s</a></h2> 86*404b540aSrobert <p>If you call <code> std::swap(x,y); </code> where x and y are standard 87*404b540aSrobert containers, then the call will automatically be replaced by a call to 88*404b540aSrobert <code> x.swap(y); </code> instead. 89*404b540aSrobert </p> 90*404b540aSrobert <p>This allows member functions of each container class to take over, and 91*404b540aSrobert containers' swap functions should have O(1) complexity according to 92*404b540aSrobert the standard. (And while "should" allows implementations to 93*404b540aSrobert behave otherwise and remain compliant, this implementation does in 94*404b540aSrobert fact use constant-time swaps.) This should not be surprising, since 95*404b540aSrobert for two containers of the same type to swap contents, only some 96*404b540aSrobert internal pointers to storage need to be exchanged. 97*404b540aSrobert </p> 98*404b540aSrobert <p>Return <a href="#top">to top of page</a> or 99*404b540aSrobert <a href="../faq/index.html">to the FAQ</a>. 100*404b540aSrobert </p> 101*404b540aSrobert 102*404b540aSrobert 103*404b540aSrobert 104*404b540aSrobert 105*404b540aSrobert<!-- ####################################################### --> 106*404b540aSrobert 107*404b540aSrobert<hr /> 108*404b540aSrobert<p class="fineprint"><em> 109*404b540aSrobertSee <a href="../17_intro/license.html">license.html</a> for copying conditions. 110*404b540aSrobertComments and suggestions are welcome, and may be sent to 111*404b540aSrobert<a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>. 112*404b540aSrobert</em></p> 113*404b540aSrobert 114*404b540aSrobert 115*404b540aSrobert</body> 116*404b540aSrobert</html> 117