]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/doc/xml/manual/iterators.xml
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / doc / xml / manual / iterators.xml
1 <?xml version='1.0'?>
2 <!DOCTYPE part PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" 
3  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" 
4 [ ]>
5
6 <part id="manual.iterators" xreflabel="Iterators">
7 <?dbhtml filename="iterators.html"?>
8  
9 <partinfo>
10   <keywordset>
11     <keyword>
12       ISO C++
13     </keyword>
14     <keyword>
15       library
16     </keyword>
17   </keywordset>
18 </partinfo>
19
20 <title>Iterators</title>
21
22 <!-- Chapter 01 : Predefined -->
23 <chapter id="manual.iterators.predefined" xreflabel="Predefined">
24   <title>Predefined</title>
25
26   <sect1 id="iterators.predefined.vs_pointers" xreflabel="Versus Pointers">
27     <title>Iterators vs. Pointers</title>
28    <para><ulink url="../faq/index.html#5_1">FAQ 5.1</ulink> points out that iterators
29       are not implemented as pointers.  They are a generalization of
30       pointers, but they are implemented in libstdc++ as separate classes.
31    </para>
32    <para>Keeping that simple fact in mind as you design your code will
33       prevent a whole lot of difficult-to-understand bugs.
34    </para>
35    <para>You can think of it the other way 'round, even.  Since iterators
36       are a generalization, that means that <emphasis>pointers</emphasis> are
37       <emphasis>iterators</emphasis>, and that pointers can be used whenever an
38       iterator would be.  All those functions in the Algorithms chapter
39       of the Standard will work just as well on plain arrays and their
40       pointers.
41    </para>
42    <para>That doesn't mean that when you pass in a pointer, it gets wrapped
43       into some special delegating iterator-to-pointer class with a layer
44       of overhead.  (If you think that's the case anywhere, you don't
45       understand templates to begin with...)  Oh, no; if you pass
46       in a pointer, then the compiler will instantiate that template
47       using T* as a type, and good old high-speed pointer arithmetic as
48       its operations, so the resulting code will be doing exactly the same
49       things as it would be doing if you had hand-coded it yourself (for
50       the 273rd time).
51    </para>
52    <para>How much overhead <emphasis>is</emphasis> there when using an iterator class?
53       Very little.  Most of the layering classes contain nothing but
54       typedefs, and typedefs are &quot;meta-information&quot; that simply
55       tell the compiler some nicknames; they don't create code.  That
56       information gets passed down through inheritance, so while the
57       compiler has to do work looking up all the names, your runtime code
58       does not.  (This has been a prime concern from the beginning.)
59    </para>
60  
61
62   </sect1>
63
64   <sect1 id="iterators.predefined.end" xreflabel="end() Is One Past the End">
65     <title>One Past the End</title>
66
67    <para>This starts off sounding complicated, but is actually very easy,
68       especially towards the end.  Trust me.
69    </para>
70    <para>Beginners usually have a little trouble understand the whole
71       'past-the-end' thing, until they remember their early algebra classes
72       (see, they <emphasis>told</emphasis> you that stuff would come in handy!) and
73       the concept of half-open ranges.
74    </para>
75    <para>First, some history, and a reminder of some of the funkier rules in
76       C and C++ for builtin arrays.  The following rules have always been
77       true for both languages:
78    </para>
79    <orderedlist>
80       <listitem>
81         <para>You can point anywhere in the array, <emphasis>or to the first element
82           past the end of the array</emphasis>.  A pointer that points to one
83           past the end of the array is guaranteed to be as unique as a
84           pointer to somewhere inside the array, so that you can compare
85           such pointers safely.
86         </para>
87       </listitem>
88       <listitem>
89         <para>You can only dereference a pointer that points into an array.
90           If your array pointer points outside the array -- even to just
91           one past the end -- and you dereference it, Bad Things happen.
92         </para>
93       </listitem>
94       <listitem>
95         <para>Strictly speaking, simply pointing anywhere else invokes
96           undefined behavior.  Most programs won't puke until such a
97           pointer is actually dereferenced, but the standards leave that
98           up to the platform.
99         </para>
100       </listitem>
101    </orderedlist>
102    <para>The reason this past-the-end addressing was allowed is to make it
103       easy to write a loop to go over an entire array, e.g.,
104       while (*d++ = *s++);.
105    </para>
106    <para>So, when you think of two pointers delimiting an array, don't think
107       of them as indexing 0 through n-1.  Think of them as <emphasis>boundary
108       markers</emphasis>:
109    </para>
110    <programlisting>
111
112    beginning            end
113      |                   |
114      |                   |               This is bad.  Always having to
115      |                   |               remember to add or subtract one.
116      |                   |               Off-by-one bugs very common here.
117      V                   V
118         array of N elements
119      |---|---|--...--|---|---|
120      | 0 | 1 |  ...  |N-2|N-1|
121      |---|---|--...--|---|---|
122
123      ^                       ^
124      |                       |
125      |                       |           This is good.  This is safe.  This
126      |                       |           is guaranteed to work.  Just don't
127      |                       |           dereference 'end'.
128    beginning                end
129
130    </programlisting>
131    <para>See?  Everything between the boundary markers is part of the array.
132       Simple.
133    </para>
134    <para>Now think back to your junior-high school algebra course, when you
135       were learning how to draw graphs.  Remember that a graph terminating
136       with a solid dot meant, &quot;Everything up through this point,&quot;
137       and a graph terminating with an open dot meant, &quot;Everything up
138       to, but not including, this point,&quot; respectively called closed
139       and open ranges?  Remember how closed ranges were written with
140       brackets, <emphasis>[a,b]</emphasis>, and open ranges were written with parentheses,
141       <emphasis>(a,b)</emphasis>?
142    </para>
143    <para>The boundary markers for arrays describe a <emphasis>half-open range</emphasis>,
144       starting with (and including) the first element, and ending with (but
145       not including) the last element:  <emphasis>[beginning,end)</emphasis>.  See, I
146       told you it would be simple in the end.
147    </para>
148    <para>Iterators, and everything working with iterators, follows this same
149       time-honored tradition.  A container's <code>begin()</code> method returns
150       an iterator referring to the first element, and its <code>end()</code>
151       method returns a past-the-end iterator, which is guaranteed to be
152       unique and comparable against any other iterator pointing into the
153       middle of the container.
154    </para>
155    <para>Container constructors, container methods, and algorithms, all take
156       pairs of iterators describing a range of values on which to operate.
157       All of these ranges are half-open ranges, so you pass the beginning
158       iterator as the starting parameter, and the one-past-the-end iterator
159       as the finishing parameter.
160    </para>
161    <para>This generalizes very well.  You can operate on sub-ranges quite
162       easily this way; functions accepting a <emphasis>[first,last)</emphasis> range
163       don't know or care whether they are the boundaries of an entire {array,
164       sequence, container, whatever}, or whether they only enclose a few
165       elements from the center.  This approach also makes zero-length
166       sequences very simple to recognize:  if the two endpoints compare
167       equal, then the {array, sequence, container, whatever} is empty.
168    </para>
169    <para>Just don't dereference <code>end()</code>.
170    </para>
171
172   </sect1>
173 </chapter>
174
175 <!-- Chapter 02 : Stream -->
176
177 </part>