50fbf0c35d
2009-07-20 Benjamin Kosnik <bkoz@redhat.com> * doc/xml/manual/intro.xml: Escape '&', validate. * doc/xml/manual/using.xml: Validate, dead link check. * doc/xml/manual/strings.xml: Same. * doc/xml/manual/appendix_contributing.xml: Same. * doc/xml/manual/iterators.xml: Same. * doc/xml/manual/spine.xml: Same. * doc/xml/faq.xml: Remove redundant xreflabel entities. * doc/xml/gnu/gpl-3.0.xml: Same. * doc/xml/manual/mt_allocator.xml: Same. * doc/xml/manual/allocator.xml: Same. * doc/xml/manual/ctype.xml: Same. * doc/xml/manual/codecvt.xml: Same. * doc/xml/manual/backwards_compatibility.xml: Same. * doc/xml/manual/shared_ptr.xml: Same. * doc/xml/manual/abi.xml: Same. * doc/xml/manual/auto_ptr.xml: Same. * doc/xml/manual/internals.xml: Same. * doc/xml/manual/parallel_mode.xml: Same. * doc/xml/manual/bitmap_allocator.xml: Same. * doc/xml/manual/build_hacking.xml: Same. * doc/xml/manual/evolution.xml: Same. * doc/xml/manual/debug.xml: Same. * doc/xml/manual/localization.xml: Same. * doc/xml/manual/appendix_contributing.xml: Same. * doc/xml/manual/locale.xml: Same. * doc/xml/manual/messages.xml: Same. * doc/xml/manual/spine.xml: Same. * doc/xml/manual/test.xml: Same. * doc/xml/book.txml: Same. * doc/xml/spine.xml: Same. * doc/xml/api.xml: Clean up ulink targets, convert to link if possible. * doc/xml/manual/backwards_compatibility.xml: Same. * doc/xml/manual/concurrency.xml: Same. * doc/xml/manual/intro.xml: Same. * doc/xml/manual/parallel_mode.xml: Same. * doc/xml/manual/status_cxx1998.xml: Same. * doc/xml/manual/containers.xml: Same. * doc/xml/manual/io.xml: Same. * doc/xml/manual/support.xml: Same. * doc/xml/manual/strings.xml: Same. * doc/xml/manual/debug_mode.xml: Same. * doc/xml/manual/extensions.xml: Same. * doc/xml/manual/appendix_contributing.xml: Same. * doc/xml/manual/messages.xml: Same. * doc/xml/manual/test.xml: Same. * doc/html: Regenerate. From-SVN: r149844
91 lines
5.9 KiB
HTML
91 lines
5.9 KiB
HTML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||
<!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 18. Interacting with C</title><meta name="generator" content="DocBook XSL Stylesheets V1.74.3" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="containers.html" title="Part VII. Containers" /><link rel="prev" href="bitset.html" title="bitset" /><link rel="next" href="iterators.html" title="Part VIII. Iterators" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 18. Interacting with C</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="bitset.html">Prev</a> </td><th width="60%" align="center">Part VII.
|
||
Containers
|
||
|
||
</th><td width="20%" align="right"> <a accesskey="n" href="iterators.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.containers.c"></a>Chapter 18. Interacting with C</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="containers_and_c.html#containers.c.vs_array">Containers vs. Arrays</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="containers.c.vs_array"></a>Containers vs. Arrays</h2></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">container</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="bk01pt08ch19s02.html" 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<typename T>
|
||
inline typename vector<T>::iterator
|
||
beginof(vector<T> &v)
|
||
{ return v.begin(); }
|
||
|
||
template<typename T, unsigned int sz>
|
||
inline T*
|
||
beginof(T (&array)[sz]) { return array; }
|
||
|
||
// endof
|
||
template<typename T>
|
||
inline typename vector<T>::iterator
|
||
endof(vector<T> &v)
|
||
{ return v.end(); }
|
||
|
||
template<typename T, unsigned int sz>
|
||
inline T*
|
||
endof(T (&array)[sz]) { return array + sz; }
|
||
|
||
// lengthof
|
||
template<typename T>
|
||
inline typename vector<T>::size_type
|
||
lengthof(vector<T> &v)
|
||
{ return v.size(); }
|
||
|
||
template<typename T, unsigned int sz>
|
||
inline unsigned int
|
||
lengthof(T (&)[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<T></code> instead
|
||
of a more general <code class="code">Container<T></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 (&)[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 width="40%" align="left"><a accesskey="p" href="bitset.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="containers.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="iterators.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">bitset </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Part VIII.
|
||
Iterators
|
||
|
||
</td></tr></table></div></body></html>
|