*: Regenerate.
[gcc.git] / libstdc++-v3 / doc / html / manual / iterators.html
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 10.  Iterators</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="
2 ISO C++
3 ,
4 library
5 "><meta name="keywords" content="
6 ISO C++
7 ,
8 runtime
9 ,
10 library
11 "><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="containers_and_c.html" title="Interacting with C"><link rel="next" href="algorithms.html" title="Chapter 11.  Algorithms"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 10
12 Iterators
13
14 </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="containers_and_c.html">Prev</a> </td><th width="60%" align="center">Part II. 
15 Standard Contents
16 </th><td width="20%" align="right"> <a accesskey="n" href="algorithms.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter 10.  Iterators"><div class="titlepage"><div><div><h2 class="title"><a name="std.iterators"></a>Chapter 10
17 Iterators
18 <a class="indexterm" name="id628227"></a>
19 </h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="iterators.html#std.iterators.predefined">Predefined</a></span></dt><dd><dl><dt><span class="section"><a href="iterators.html#iterators.predefined.vs_pointers">Iterators vs. Pointers</a></span></dt><dt><span class="section"><a href="iterators.html#iterators.predefined.end">One Past the End</a></span></dt></dl></dd></dl></div><div class="section" title="Predefined"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="std.iterators.predefined"></a>Predefined</h2></div></div></div><div class="section" title="Iterators vs. Pointers"><div class="titlepage"><div><div><h3 class="title"><a name="iterators.predefined.vs_pointers"></a>Iterators vs. Pointers</h3></div></div></div><p>
20 The following
21 FAQ <a class="link" href="../faq.html#faq.iterator_as_pod" title="7.1.">entry</a> points out that
22 iterators are not implemented as pointers. They are a generalization
23 of pointers, but they are implemented in libstdc++ as separate
24 classes.
25 </p><p>
26 Keeping that simple fact in mind as you design your code will
27 prevent a whole lot of difficult-to-understand bugs.
28 </p><p>
29 You can think of it the other way 'round, even. Since iterators
30 are a generalization, that means
31 that <span class="emphasis"><em>pointers</em></span> are
32 <span class="emphasis"><em>iterators</em></span>, and that pointers can be used
33 whenever an iterator would be. All those functions in the
34 Algorithms section of the Standard will work just as well on plain
35 arrays and their pointers.
36 </p><p>
37 That doesn't mean that when you pass in a pointer, it gets
38 wrapped into some special delegating iterator-to-pointer class
39 with a layer of overhead. (If you think that's the case
40 anywhere, you don't understand templates to begin with...) Oh,
41 no; if you pass in a pointer, then the compiler will instantiate
42 that template using T* as a type, and good old high-speed
43 pointer arithmetic as its operations, so the resulting code will
44 be doing exactly the same things as it would be doing if you had
45 hand-coded it yourself (for the 273rd time).
46 </p><p>
47 How much overhead <span class="emphasis"><em>is</em></span> there when using an
48 iterator class? Very little. Most of the layering classes
49 contain nothing but typedefs, and typedefs are
50 "meta-information" that simply tell the compiler some
51 nicknames; they don't create code. That information gets passed
52 down through inheritance, so while the compiler has to do work
53 looking up all the names, your runtime code does not. (This has
54 been a prime concern from the beginning.)
55 </p></div><div class="section" title="One Past the End"><div class="titlepage"><div><div><h3 class="title"><a name="iterators.predefined.end"></a>One Past the End</h3></div></div></div><p>This starts off sounding complicated, but is actually very easy,
56 especially towards the end. Trust me.
57 </p><p>Beginners usually have a little trouble understand the whole
58 'past-the-end' thing, until they remember their early algebra classes
59 (see, they <span class="emphasis"><em>told</em></span> you that stuff would come in handy!) and
60 the concept of half-open ranges.
61 </p><p>First, some history, and a reminder of some of the funkier rules in
62 C and C++ for builtin arrays. The following rules have always been
63 true for both languages:
64 </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>You can point anywhere in the array, <span class="emphasis"><em>or to the first element
65 past the end of the array</em></span>. A pointer that points to one
66 past the end of the array is guaranteed to be as unique as a
67 pointer to somewhere inside the array, so that you can compare
68 such pointers safely.
69 </p></li><li class="listitem"><p>You can only dereference a pointer that points into an array.
70 If your array pointer points outside the array -- even to just
71 one past the end -- and you dereference it, Bad Things happen.
72 </p></li><li class="listitem"><p>Strictly speaking, simply pointing anywhere else invokes
73 undefined behavior. Most programs won't puke until such a
74 pointer is actually dereferenced, but the standards leave that
75 up to the platform.
76 </p></li></ol></div><p>The reason this past-the-end addressing was allowed is to make it
77 easy to write a loop to go over an entire array, e.g.,
78 while (*d++ = *s++);.
79 </p><p>So, when you think of two pointers delimiting an array, don't think
80 of them as indexing 0 through n-1. Think of them as <span class="emphasis"><em>boundary
81 markers</em></span>:
82 </p><pre class="programlisting">
83
84 beginning end
85 | |
86 | | This is bad. Always having to
87 | | remember to add or subtract one.
88 | | Off-by-one bugs very common here.
89 V V
90 array of N elements
91 |---|---|--...--|---|---|
92 | 0 | 1 | ... |N-2|N-1|
93 |---|---|--...--|---|---|
94
95 ^ ^
96 | |
97 | | This is good. This is safe. This
98 | | is guaranteed to work. Just don't
99 | | dereference 'end'.
100 beginning end
101
102 </pre><p>See? Everything between the boundary markers is chapter of the array.
103 Simple.
104 </p><p>Now think back to your junior-high school algebra course, when you
105 were learning how to draw graphs. Remember that a graph terminating
106 with a solid dot meant, "Everything up through this point,"
107 and a graph terminating with an open dot meant, "Everything up
108 to, but not including, this point," respectively called closed
109 and open ranges? Remember how closed ranges were written with
110 brackets, <span class="emphasis"><em>[a,b]</em></span>, and open ranges were written with parentheses,
111 <span class="emphasis"><em>(a,b)</em></span>?
112 </p><p>The boundary markers for arrays describe a <span class="emphasis"><em>half-open range</em></span>,
113 starting with (and including) the first element, and ending with (but
114 not including) the last element: <span class="emphasis"><em>[beginning,end)</em></span>. See, I
115 told you it would be simple in the end.
116 </p><p>Iterators, and everything working with iterators, follows this same
117 time-honored tradition. A container's <code class="code">begin()</code> method returns
118 an iterator referring to the first element, and its <code class="code">end()</code>
119 method returns a past-the-end iterator, which is guaranteed to be
120 unique and comparable against any other iterator pointing into the
121 middle of the container.
122 </p><p>Container constructors, container methods, and algorithms, all take
123 pairs of iterators describing a range of values on which to operate.
124 All of these ranges are half-open ranges, so you pass the beginning
125 iterator as the starting parameter, and the one-past-the-end iterator
126 as the finishing parameter.
127 </p><p>This generalizes very well. You can operate on sub-ranges quite
128 easily this way; functions accepting a <span class="emphasis"><em>[first,last)</em></span> range
129 don't know or care whether they are the boundaries of an entire {array,
130 sequence, container, whatever}, or whether they only enclose a few
131 elements from the center. This approach also makes zero-length
132 sequences very simple to recognize: if the two endpoints compare
133 equal, then the {array, sequence, container, whatever} is empty.
134 </p><p>Just don't dereference <code class="code">end()</code>.
135 </p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="containers_and_c.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="bk01pt02.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="algorithms.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Interacting with C </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 11
136 Algorithms
137
138 </td></tr></table></div></body></html>