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 18. Interacting with C</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="containers.html" title="Part VII. Containers" /><link rel="prev" href="bk01pt07ch17s02.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="bk01pt07ch17s02.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="bk01pt07ch18.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>
4 You're writing some code and can't decide whether to use builtin
5 arrays or some kind of container. There are compelling reasons
6 to use one of the container classes, but you're afraid that
7 you'll eventually run into difficulties, change everything back
8 to arrays, and then have to change all the code that uses those
9 data types to keep up with the change.
11 If your code makes use of the standard algorithms, this isn't as
12 scary as it sounds. The algorithms don't know, nor care, about
13 the kind of “<span class="quote">container</span>” on which they work, since
14 the algorithms are only given endpoints to work with. For the
15 container classes, these are iterators (usually
16 <code class="code">begin()</code> and <code class="code">end()</code>, but not always).
17 For builtin arrays, these are the address of the first element
18 and the <a class="ulink" href="../24_iterators/howto.html#2" target="_top">past-the-end</a> element.
20 Some very simple wrapper functions can hide all of that from the
21 rest of the code. For example, a pair of functions called
22 <code class="code">beginof</code> can be written, one that takes an array,
23 another that takes a vector. The first returns a pointer to the
24 first element, and the second returns the vector's
25 <code class="code">begin()</code> iterator.
27 The functions should be made template functions, and should also
28 be declared inline. As pointed out in the comments in the code
29 below, this can lead to <code class="code">beginof</code> being optimized out
30 of existence, so you pay absolutely nothing in terms of increased
31 code size or execution time.
33 The result is that if all your algorithm calls look like
34 </p><pre class="programlisting">
35 std::transform(beginof(foo), endof(foo), beginof(foo), SomeFunction);
37 then the type of foo can change from an array of ints to a vector
38 of ints to a deque of ints and back again, without ever changing
41 This author has a collection of such functions, called
42 “<span class="quote">*of</span>” because they all extend the builtin
43 “<span class="quote">sizeof</span>”. It started with some Usenet discussions
44 on a transparent way to find the length of an array. A
45 simplified and much-reduced version for easier reading is <a class="ulink" href="wrappers_h.txt" target="_top">given here</a>.
47 Astute readers will notice two things at once: first, that the
48 container class is still a <code class="code">vector<T></code> instead
49 of a more general <code class="code">Container<T></code>. This would
50 mean that three functions for <code class="code">deque</code> would have to be
51 added, another three for <code class="code">list</code>, and so on. This is
52 due to problems with getting template resolution correct; I find
53 it easier just to give the extra three lines and avoid confusion.
56 </p><pre class="programlisting">
57 inline unsigned int lengthof (T (&)[sz]) { return sz; }
59 looks just weird! Hint: unused parameters can be left nameless.
60 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk01pt07ch17s02.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>