summaryrefslogtreecommitdiff
path: root/libstdc++-v3/doc/html/manual/containers_and_c.html
diff options
context:
space:
mode:
authorupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
committerupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
commit554fd8c5195424bdbcabf5de30fdc183aba391bd (patch)
tree976dc5ab7fddf506dadce60ae936f43f58787092 /libstdc++-v3/doc/html/manual/containers_and_c.html
downloadcbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2
cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig; imported gcc-4.6.4 source tree from verified upstream tarball. downloading a git-generated archive based on the 'upstream' tag should provide you with a source tree that is binary identical to the one extracted from the above tarball. if you have obtained the source via the command 'git clone', however, do note that line-endings of files in your working directory might differ from line-endings of the respective files in the upstream repository.
Diffstat (limited to 'libstdc++-v3/doc/html/manual/containers_and_c.html')
-rw-r--r--libstdc++-v3/doc/html/manual/containers_and_c.html90
1 files changed, 90 insertions, 0 deletions
diff --git a/libstdc++-v3/doc/html/manual/containers_and_c.html b/libstdc++-v3/doc/html/manual/containers_and_c.html
new file mode 100644
index 000000000..3e612aa5a
--- /dev/null
+++ b/libstdc++-v3/doc/html/manual/containers_and_c.html
@@ -0,0 +1,90 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Interacting with C</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"/><meta name="keywords" content="&#10; ISO C++&#10; , &#10; library&#10; "/><link rel="home" href="../spine.html" title="The GNU C++ Library"/><link rel="up" href="containers.html" title="Chapter 9.  Containers"/><link rel="prev" href="associative.html" title="Associative"/><link rel="next" href="iterators.html" title="Chapter 10.  Iterators"/></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Interacting with C</th></tr><tr><td align="left"><a accesskey="p" href="associative.html">Prev</a> </td><th width="60%" align="center">Chapter 9. 
+ Containers
+
+</th><td align="right"> <a accesskey="n" href="iterators.html">Next</a></td></tr></table><hr/></div><div class="section" title="Interacting with C"><div class="titlepage"><div><div><h2 class="title"><a id="std.containers.c"/>Interacting with C</h2></div></div></div><div class="section" title="Containers vs. Arrays"><div class="titlepage"><div><div><h3 class="title"><a id="containers.c.vs_array"/>Containers vs. Arrays</h3></div></div></div><p>
+ You're writing some code and can't decide whether to use builtin
+ arrays or some kind of container. There are compelling reasons
+ to use one of the container classes, but you're afraid that
+ you'll eventually run into difficulties, change everything back
+ to arrays, and then have to change all the code that uses those
+ data types to keep up with the change.
+ </p><p>
+ If your code makes use of the standard algorithms, this isn't as
+ scary as it sounds. The algorithms don't know, nor care, about
+ the kind of <span class="quote">“<span class="quote">container</span>”</span> on which they work, since
+ the algorithms are only given endpoints to work with. For the
+ container classes, these are iterators (usually
+ <code class="code">begin()</code> and <code class="code">end()</code>, but not always).
+ For builtin arrays, these are the address of the first element
+ and the <a class="link" href="iterators.html#iterators.predefined.end" title="One Past the End">past-the-end</a> element.
+ </p><p>
+ Some very simple wrapper functions can hide all of that from the
+ rest of the code. For example, a pair of functions called
+ <code class="code">beginof</code> can be written, one that takes an array,
+ another that takes a vector. The first returns a pointer to the
+ first element, and the second returns the vector's
+ <code class="code">begin()</code> iterator.
+ </p><p>
+ The functions should be made template functions, and should also
+ be declared inline. As pointed out in the comments in the code
+ below, this can lead to <code class="code">beginof</code> being optimized out
+ of existence, so you pay absolutely nothing in terms of increased
+ code size or execution time.
+ </p><p>
+ The result is that if all your algorithm calls look like
+ </p><pre class="programlisting">
+ std::transform(beginof(foo), endof(foo), beginof(foo), SomeFunction);
+ </pre><p>
+ then the type of foo can change from an array of ints to a vector
+ of ints to a deque of ints and back again, without ever changing
+ any client code.
+ </p><pre class="programlisting">
+// beginof
+template&lt;typename T&gt;
+ inline typename vector&lt;T&gt;::iterator
+ beginof(vector&lt;T&gt; &amp;v)
+ { return v.begin(); }
+
+template&lt;typename T, unsigned int sz&gt;
+ inline T*
+ beginof(T (&amp;array)[sz]) { return array; }
+
+// endof
+template&lt;typename T&gt;
+ inline typename vector&lt;T&gt;::iterator
+ endof(vector&lt;T&gt; &amp;v)
+ { return v.end(); }
+
+template&lt;typename T, unsigned int sz&gt;
+ inline T*
+ endof(T (&amp;array)[sz]) { return array + sz; }
+
+// lengthof
+template&lt;typename T&gt;
+ inline typename vector&lt;T&gt;::size_type
+ lengthof(vector&lt;T&gt; &amp;v)
+ { return v.size(); }
+
+template&lt;typename T, unsigned int sz&gt;
+ inline unsigned int
+ lengthof(T (&amp;)[sz]) { return sz; }
+</pre><p>
+ Astute readers will notice two things at once: first, that the
+ container class is still a <code class="code">vector&lt;T&gt;</code> instead
+ of a more general <code class="code">Container&lt;T&gt;</code>. This would
+ mean that three functions for <code class="code">deque</code> would have to be
+ added, another three for <code class="code">list</code>, and so on. This is
+ due to problems with getting template resolution correct; I find
+ it easier just to give the extra three lines and avoid confusion.
+ </p><p>
+ Second, the line
+ </p><pre class="programlisting">
+ inline unsigned int lengthof (T (&amp;)[sz]) { return sz; }
+ </pre><p>
+ looks just weird! Hint: unused parameters can be left nameless.
+ </p></div></div><div class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td align="left"><a accesskey="p" href="associative.html">Prev</a> </td><td align="center"><a accesskey="u" href="containers.html">Up</a></td><td align="right"> <a accesskey="n" href="iterators.html">Next</a></td></tr><tr><td align="left" valign="top">Associative </td><td align="center"><a accesskey="h" href="../spine.html">Home</a></td><td align="right" valign="top"> Chapter 10. 
+ Iterators
+
+</td></tr></table></div></body></html>