1 |
742 |
jeremybenn |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
2 |
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
3 |
|
|
<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=" ISO C++ , library "/><meta name="keywords" content=" ISO C++ , runtime , library "/><link rel="home" href="../index.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.
|
4 |
|
|
Containers
|
5 |
|
|
|
6 |
|
|
</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>
|
7 |
|
|
You're writing some code and can't decide whether to use builtin
|
8 |
|
|
arrays or some kind of container. There are compelling reasons
|
9 |
|
|
to use one of the container classes, but you're afraid that
|
10 |
|
|
you'll eventually run into difficulties, change everything back
|
11 |
|
|
to arrays, and then have to change all the code that uses those
|
12 |
|
|
data types to keep up with the change.
|
13 |
|
|
</p><p>
|
14 |
|
|
If your code makes use of the standard algorithms, this isn't as
|
15 |
|
|
scary as it sounds. The algorithms don't know, nor care, about
|
16 |
|
|
the kind of <span class="quote">“<span class="quote">container</span>”</span> on which they work, since
|
17 |
|
|
the algorithms are only given endpoints to work with. For the
|
18 |
|
|
container classes, these are iterators (usually
|
19 |
|
|
<code class="code">begin()</code> and <code class="code">end()</code>, but not always).
|
20 |
|
|
For builtin arrays, these are the address of the first element
|
21 |
|
|
and the <a class="link" href="iterators.html#iterators.predefined.end" title="One Past the End">past-the-end</a> element.
|
22 |
|
|
</p><p>
|
23 |
|
|
Some very simple wrapper functions can hide all of that from the
|
24 |
|
|
rest of the code. For example, a pair of functions called
|
25 |
|
|
<code class="code">beginof</code> can be written, one that takes an array,
|
26 |
|
|
another that takes a vector. The first returns a pointer to the
|
27 |
|
|
first element, and the second returns the vector's
|
28 |
|
|
<code class="code">begin()</code> iterator.
|
29 |
|
|
</p><p>
|
30 |
|
|
The functions should be made template functions, and should also
|
31 |
|
|
be declared inline. As pointed out in the comments in the code
|
32 |
|
|
below, this can lead to <code class="code">beginof</code> being optimized out
|
33 |
|
|
of existence, so you pay absolutely nothing in terms of increased
|
34 |
|
|
code size or execution time.
|
35 |
|
|
</p><p>
|
36 |
|
|
The result is that if all your algorithm calls look like
|
37 |
|
|
</p><pre class="programlisting">
|
38 |
|
|
std::transform(beginof(foo), endof(foo), beginof(foo), SomeFunction);
|
39 |
|
|
</pre><p>
|
40 |
|
|
then the type of foo can change from an array of ints to a vector
|
41 |
|
|
of ints to a deque of ints and back again, without ever changing
|
42 |
|
|
any client code.
|
43 |
|
|
</p><pre class="programlisting">
|
44 |
|
|
// beginof
|
45 |
|
|
template<typename T>
|
46 |
|
|
inline typename vector<T>::iterator
|
47 |
|
|
beginof(vector<T> &v)
|
48 |
|
|
{ return v.begin(); }
|
49 |
|
|
|
50 |
|
|
template<typename T, unsigned int sz>
|
51 |
|
|
inline T*
|
52 |
|
|
beginof(T (&array)[sz]) { return array; }
|
53 |
|
|
|
54 |
|
|
// endof
|
55 |
|
|
template<typename T>
|
56 |
|
|
inline typename vector<T>::iterator
|
57 |
|
|
endof(vector<T> &v)
|
58 |
|
|
{ return v.end(); }
|
59 |
|
|
|
60 |
|
|
template<typename T, unsigned int sz>
|
61 |
|
|
inline T*
|
62 |
|
|
endof(T (&array)[sz]) { return array + sz; }
|
63 |
|
|
|
64 |
|
|
// lengthof
|
65 |
|
|
template<typename T>
|
66 |
|
|
inline typename vector<T>::size_type
|
67 |
|
|
lengthof(vector<T> &v)
|
68 |
|
|
{ return v.size(); }
|
69 |
|
|
|
70 |
|
|
template<typename T, unsigned int sz>
|
71 |
|
|
inline unsigned int
|
72 |
|
|
lengthof(T (&)[sz]) { return sz; }
|
73 |
|
|
</pre><p>
|
74 |
|
|
Astute readers will notice two things at once: first, that the
|
75 |
|
|
container class is still a <code class="code">vector<T></code> instead
|
76 |
|
|
of a more general <code class="code">Container<T></code>. This would
|
77 |
|
|
mean that three functions for <code class="code">deque</code> would have to be
|
78 |
|
|
added, another three for <code class="code">list</code>, and so on. This is
|
79 |
|
|
due to problems with getting template resolution correct; I find
|
80 |
|
|
it easier just to give the extra three lines and avoid confusion.
|
81 |
|
|
</p><p>
|
82 |
|
|
Second, the line
|
83 |
|
|
</p><pre class="programlisting">
|
84 |
|
|
inline unsigned int lengthof (T (&)[sz]) { return sz; }
|
85 |
|
|
</pre><p>
|
86 |
|
|
looks just weird! Hint: unused parameters can be left nameless.
|
87 |
|
|
</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="../index.html">Home</a></td><td align="right" valign="top"> Chapter 10.
|
88 |
|
|
Iterators
|
89 |
|
|
|
90 |
|
|
</td></tr></table></div></body></html>
|