OSDN Git Service

f54749465351849f3b4efb10558e9ac5d19b0a21
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / html / manual / bk01pt08ch19.html
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.3" /><meta name="keywords" content="&#10;      ISO C++&#10;    , &#10;      library&#10;    " /><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. 
4   Iterators
5   
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>
7      The following
8 FAQ <a class="link" href="../faq.html#faq.iterator_as_pod" title="Question">entry</a> points out that
9 iterators are not implemented as pointers.  They are a generalization
10 of pointers, but they are implemented in libstdc++ as separate
11 classes.
12    </p><p>Keeping that simple fact in mind as you design your code will
13       prevent a whole lot of difficult-to-understand bugs.
14    </p><p>You can think of it the other way 'round, even.  Since iterators
15       are a generalization, that means that <span class="emphasis"><em>pointers</em></span> are
16       <span class="emphasis"><em>iterators</em></span>, and that pointers can be used whenever an
17       iterator would be.  All those functions in the Algorithms chapter
18       of the Standard will work just as well on plain arrays and their
19       pointers.
20    </p><p>That doesn't mean that when you pass in a pointer, it gets wrapped
21       into some special delegating iterator-to-pointer class with a layer
22       of overhead.  (If you think that's the case anywhere, you don't
23       understand templates to begin with...)  Oh, no; if you pass
24       in a pointer, then the compiler will instantiate that template
25       using T* as a type, and good old high-speed pointer arithmetic as
26       its operations, so the resulting code will be doing exactly the same
27       things as it would be doing if you had hand-coded it yourself (for
28       the 273rd time).
29    </p><p>How much overhead <span class="emphasis"><em>is</em></span> there when using an iterator class?
30       Very little.  Most of the layering classes contain nothing but
31       typedefs, and typedefs are "meta-information" that simply
32       tell the compiler some nicknames; they don't create code.  That
33       information gets passed down through inheritance, so while the
34       compiler has to do work looking up all the names, your runtime code
35       does not.  (This has been a prime concern from the beginning.)
36    </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. 
37   Iterators
38   
39  </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>