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