1 |
742 |
jeremybenn |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
2 |
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
3 |
|
|
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Chapter 11. Algorithms</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"/><meta name="keywords" content=" ISO C++ , library , algorithm "/><meta name="keywords" content=" ISO C++ , runtime , library "/><link rel="home" href="../index.html" title="The GNU C++ Library"/><link rel="up" href="bk01pt02.html" title="Part II. Standard Contents"/><link rel="prev" href="iterators.html" title="Chapter 10. Iterators"/><link rel="next" href="numerics.html" title="Chapter 12. Numerics"/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 11.
|
4 |
|
|
Algorithms
|
5 |
|
|
|
6 |
|
|
</th></tr><tr><td align="left"><a accesskey="p" href="iterators.html">Prev</a> </td><th width="60%" align="center">Part II.
|
7 |
|
|
Standard Contents
|
8 |
|
|
</th><td align="right"> <a accesskey="n" href="numerics.html">Next</a></td></tr></table><hr/></div><div class="chapter" title="Chapter 11. Algorithms"><div class="titlepage"><div><div><h2 class="title"><a id="std.algorithms"/>Chapter 11.
|
9 |
|
|
Algorithms
|
10 |
|
|
<a id="id504394" class="indexterm"/>
|
11 |
|
|
</h2></div></div></div><div class="toc"><p><strong>Table of Contents</strong></p><dl><dt><span class="section"><a href="algorithms.html#std.algorithms.mutating">Mutating</a></span></dt><dd><dl><dt><span class="section"><a href="algorithms.html#algorithms.mutating.swap"><code class="function">swap</code></a></span></dt><dd><dl><dt><span class="section"><a href="algorithms.html#algorithms.swap.specializations">Specializations</a></span></dt></dl></dd></dl></dd></dl></div><p>
|
12 |
|
|
The neatest accomplishment of the algorithms section is that all the
|
13 |
|
|
work is done via iterators, not containers directly. This means two
|
14 |
|
|
important things:
|
15 |
|
|
</p><div class="orderedlist"><ol class="orderedlist"><li class="listitem"><p>
|
16 |
|
|
Anything that behaves like an iterator can be used in one of
|
17 |
|
|
these algorithms. Raw pointers make great candidates, thus
|
18 |
|
|
built-in arrays are fine containers, as well as your own
|
19 |
|
|
iterators.
|
20 |
|
|
</p></li><li class="listitem"><p>
|
21 |
|
|
The algorithms do not (and cannot) affect the container as a
|
22 |
|
|
whole; only the things between the two iterator endpoints. If
|
23 |
|
|
you pass a range of iterators only enclosing the middle third of
|
24 |
|
|
a container, then anything outside that range is inviolate.
|
25 |
|
|
</p></li></ol></div><p>
|
26 |
|
|
Even strings can be fed through the algorithms here, although the
|
27 |
|
|
string class has specialized versions of many of these functions
|
28 |
|
|
(for example, <code class="code">string::find()</code>). Most of the examples
|
29 |
|
|
on this page will use simple arrays of integers as a playground
|
30 |
|
|
for algorithms, just to keep things simple. The use of
|
31 |
|
|
<span class="emphasis"><em>N</em></span> as a size in the examples is to keep things
|
32 |
|
|
easy to read but probably won't be valid code. You can use wrappers
|
33 |
|
|
such as those described in
|
34 |
|
|
the <a class="link" href="containers.html" title="Chapter 9. Containers">containers section</a> to keep
|
35 |
|
|
real code readable.
|
36 |
|
|
</p><p>
|
37 |
|
|
The single thing that trips people up the most is the definition
|
38 |
|
|
of <span class="emphasis"><em>range</em></span> used with iterators; the famous
|
39 |
|
|
"past-the-end" rule that everybody loves to hate. The
|
40 |
|
|
<a class="link" href="iterators.html" title="Chapter 10. Iterators">iterators section</a> of this
|
41 |
|
|
document has a complete explanation of this simple rule that seems
|
42 |
|
|
to cause so much confusion. Once you
|
43 |
|
|
get <span class="emphasis"><em>range</em></span> into your head (it's not that hard,
|
44 |
|
|
honest!), then the algorithms are a cakewalk.
|
45 |
|
|
</p><div class="section" title="Mutating"><div class="titlepage"><div><div><h2 class="title"><a id="std.algorithms.mutating"/>Mutating</h2></div></div></div><div class="section" title="swap"><div class="titlepage"><div><div><h3 class="title"><a id="algorithms.mutating.swap"/><code class="function">swap</code></h3></div></div></div><div class="section" title="Specializations"><div class="titlepage"><div><div><h4 class="title"><a id="algorithms.swap.specializations"/>Specializations</h4></div></div></div><p>If you call <code class="code"> std::swap(x,y); </code> where x and y are standard
|
46 |
|
|
containers, then the call will automatically be replaced by a call to
|
47 |
|
|
<code class="code"> x.swap(y); </code> instead.
|
48 |
|
|
</p><p>This allows member functions of each container class to take over, and
|
49 |
|
|
containers' swap functions should have O(1) complexity according to
|
50 |
|
|
the standard. (And while "should" allows implementations to
|
51 |
|
|
behave otherwise and remain compliant, this implementation does in
|
52 |
|
|
fact use constant-time swaps.) This should not be surprising, since
|
53 |
|
|
for two containers of the same type to swap contents, only some
|
54 |
|
|
internal pointers to storage need to be exchanged.
|
55 |
|
|
</p></div></div></div></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td align="left"><a accesskey="p" href="iterators.html">Prev</a> </td><td align="center"><a accesskey="u" href="bk01pt02.html">Up</a></td><td align="right"> <a accesskey="n" href="numerics.html">Next</a></td></tr><tr><td align="left" valign="top">Chapter 10.
|
56 |
|
|
Iterators
|
57 |
|
|
|
58 |
|
|
</td><td align="center"><a accesskey="h" href="../index.html">Home</a></td><td align="right" valign="top"> Chapter 12.
|
59 |
|
|
Numerics
|
60 |
|
|
|
61 |
|
|
</td></tr></table></div></body></html>
|