1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 19. Predefined</title><meta name="generator" content="DocBook XSL Stylesheets V1.74.0" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="iterators.html" title="Part VIII. Iterators" /><link rel="prev" href="iterators.html" title="Part VIII. Iterators" /><link rel="next" href="bk01pt08ch19s02.html" title="One Past the End" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 19. Predefined</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="iterators.html">Prev</a> </td><th width="60%" align="center">Part VIII.
6 </th><td width="20%" align="right"> <a accesskey="n" href="bk01pt08ch19s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title"><a id="manual.iterators.predefined"></a>Chapter 19. Predefined</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="bk01pt08ch19.html#iterators.predefined.vs_pointers">Iterators vs. Pointers</a></span></dt><dt><span class="sect1"><a href="bk01pt08ch19s02.html">One Past the End</a></span></dt></dl></div><div class="sect1" lang="en" xml:lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="iterators.predefined.vs_pointers"></a>Iterators vs. Pointers</h2></div></div></div><p><a class="ulink" href="../faq/index.html#5_1" target="_top">FAQ 5.1</a> points out that iterators
7 are not implemented as pointers. They are a generalization of
8 pointers, but they are implemented in libstdc++ as separate classes.
9 </p><p>Keeping that simple fact in mind as you design your code will
10 prevent a whole lot of difficult-to-understand bugs.
11 </p><p>You can think of it the other way 'round, even. Since iterators
12 are a generalization, that means that <span class="emphasis"><em>pointers</em></span> are
13 <span class="emphasis"><em>iterators</em></span>, and that pointers can be used whenever an
14 iterator would be. All those functions in the Algorithms chapter
15 of the Standard will work just as well on plain arrays and their
17 </p><p>That doesn't mean that when you pass in a pointer, it gets wrapped
18 into some special delegating iterator-to-pointer class with a layer
19 of overhead. (If you think that's the case anywhere, you don't
20 understand templates to begin with...) Oh, no; if you pass
21 in a pointer, then the compiler will instantiate that template
22 using T* as a type, and good old high-speed pointer arithmetic as
23 its operations, so the resulting code will be doing exactly the same
24 things as it would be doing if you had hand-coded it yourself (for
26 </p><p>How much overhead <span class="emphasis"><em>is</em></span> there when using an iterator class?
27 Very little. Most of the layering classes contain nothing but
28 typedefs, and typedefs are "meta-information" that simply
29 tell the compiler some nicknames; they don't create code. That
30 information gets passed down through inheritance, so while the
31 compiler has to do work looking up all the names, your runtime code
32 does not. (This has been a prime concern from the beginning.)
33 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="iterators.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="iterators.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="bk01pt08ch19s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Part VIII.
36 </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> One Past the End</td></tr></table></div></body></html>