    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Are You Afraid of The Dark? - Making Sense of the
STL</title>
        <link>https://members.accu.org/index.php/journals/443</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #43 - Jun 2001 + Programming Topics</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c161/">43</a>
                    (7)
<br />

                                            <a href="https://members.accu.org/index.php/journals/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c161-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c161+65/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Are You Afraid of The Dark? - Making Sense of the
STL</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 June 2001 17:46:06 +01:00 or Tue, 26 June 2001 17:46:06 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<p>Do you peer in trepidation at the monsters lurking in the C++
Standard header files when you see code using Standard Template
Library techniques? Or walk swiftly by, trying to ignore them? Do
you sometimes wish you had a good, sturdy torch to have a good
look, when your trusty old pen-light won't do? OK, well, join us
for a while. Some of you may have taken Alan Griffiths' tour of the
&quot;Heights of Exception Safety&quot; [<a href=
"#Griffiths2000">Griffiths2000</a>] in the mountains above us, and
some of you may be on your first visit to this region, so welcome!
These caverns are often dark and mysterious to new visitors, and
the lighting conditions are often poor, but we've got good, strong
torches for you. Don't be afraid of the dark, because you'll find
it's not really that dark, and once you've passed this way once,
your footing will be more confident and secure when you visit
again, and I hope many of you first-timers will come by more
often.</p>
<p>There are several caverns that we'll see on our trip, and on
this first visit we'll be looking at some of the main features
exhibited, rather than a detailed examination of them. We won't get
to look at some of the harder to reach caves, which are perhaps
best left to experts. For today, our itinerary will encompass just
the main features: Containers and Iterators, Functors, and
Algorithms.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e27" id="d0e27"></a>Stalagmites and
Stalactites: Containers and Iterators</h2>
</div>
<p>All substantial programs require collections of things; a list
of employees, a stack of windows; a set of indices. These
collections are handled by container classes, that is, classes that
manage collections of other objects, and provide different methods
of accessing those objects.</p>
<p>The C++ Standard Library [<a href="#ISO1998">ISO1998</a>]
provides seven main container classes, each with different
characteristics. They are broadly divided into two sections:
sequences and associative containers [<a href=
"#Austern1998">Austern1998</a>]</p>
<div class="table"><a name="d0e40" id="d0e40"></a>
<p class="title c2">Table 1. Sequences</p>
<table summary="Sequences" border="1" cellspacing="0">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td><tt class="classname">list</tt></td>
<td>A doubly-linked list</td>
<td><tt class="literal">#include&lt;list&gt;</tt></td>
</tr>
<tr>
<td><tt class="classname">vector</tt></td>
<td>A dynamic array</td>
<td><tt class="literal">#include &lt;vector&gt;</tt></td>
</tr>
<tr>
<td><tt class="classname">deque</tt></td>
<td>A dynamic array with some special properties</td>
<td><tt class="literal">#include &lt;deque&gt;</tt></td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<div class="table"><a name="d0e72" id="d0e72"></a>
<p class="title c2">Table 2. Associative Containers</p>
<table summary="Associative Containers" border="1" cellspacing="0">
&lt;colgroup&gt;
&lt;col&gt;
&lt;col&gt;
&lt;col&gt;&lt;/colgroup&gt;
&lt;tbody&gt;
<tr>
<td><tt class="classname">map</tt></td>
<td>A collection of unique elements sorted by a key</td>
<td><tt class="literal">#include &lt;map&gt;</tt></td>
</tr>
<tr>
<td><tt class="classname">multimap</tt></td>
<td>Like map, but allows duplicates</td>
<td><tt class="literal">#include &lt;map&gt;</tt></td>
</tr>
<tr>
<td><tt class="classname">set</tt></td>
<td>A collection of unique elements sorted by value</td>
<td><tt class="literal">#include &lt;set&gt;</tt></td>
</tr>
<tr>
<td><tt class="classname">multiset</tt></td>
<td>Like set but allows duplicate elements</td>
<td><tt class="literal">#include &lt;set&gt;</tt></td>
</tr>
&lt;/tbody&gt;
</table>
</div>
<p>First of all, let's examine the characteristics that all of the
containers have in common. The first and most important aspect is
that they all own their objects. When you add an element to a
container, it is copied directly, so that you now have two such
objects - the original and the contained one. This is very
important for two reasons: firstly, you can safely get rid of the
original, or over-write it, without affecting the contained
version; second, the container automatically deletes its contained
elements when the container itself is deleted or goes out of scope.
This imposes some restrictions on how elements behave, and some
containers impose more restrictions than others.</p>
<p>Related to this is the fact that all the containers are able to
grow dynamically in memory as more space is required. Each of the
sequence containers does this differently in ways which are
important to understand.</p>
<p>All containers rely on their elements being copy-constructible
(having a valid and visible copy constructor), because the
containers manage value objects. They expect that when an object is
copied, a second instance of that object is made, rather than a
reference to some original object. This concept becomes harder to
manage when you consider containers of pointers, but we will not
explore that path at the moment.</p>
<p>The other main aspect that all container classes have in common
is iterators. These are a mechanism whereby the navigation of the
elements in a container is separate from the container itself. The
important thing to all this is that iterators from any of the
containers look the same when used for simple operations like
getting to an element's value. We won't get very far without
encountering iterators in this trip, as you'll see.</p>
<p>We don't have time to examine the associative containers here,
because a cursory overview would be more cryptic than helpful.
Let's go right ahead and look at the sequence containers.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e123" id="d0e123"></a>Sequence
Containers</h2>
</div>
<p>Each of the three sequence containers has different properties
and idiosyncrasies. As their name suggests, they handle elements as
a simple, linear sequence, with a start position and an end
position. These elements are not sorted by their respective values,
and in fact, sorting the elements is generally &quot;expensive&quot; in terms
of code efficiency. Notwithstanding this, it is possible to sort a
sequence container.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e128" id="d0e128"></a>list</h3>
</div>
<p>The list container is perhaps the simplest of all, because it
imposes the fewest restrictions on how elements are handled. What
this means in practice is that elements can be inserted anywhere in
the container easily, and efficiently. Efficiency is an important
aspect of the Standard Library in general, and the C++ Standard
makes certain guarantees about how efficient operations on
containers and algorithms are. For now, it is enough to know that
list allows efficient insertion of elements at the beginning, the
end and anywhere in between. Here is an example of how a list might
be used<sup>[<a name="d0e133" href="#ftn.d0e133" id=
"d0e133">1</a>]</sup>:</p>
<pre class="programlisting">
list&lt; string &gt; names;
string name;
while( cin &gt;&gt; name &amp;&amp; name != &quot;done&quot; )
{
  names.push_back( name );
}
list&lt; string &gt;::iterator i; 
for( i = names.begin(); 
      i != names.end(); ++i )
{
  cout &lt;&lt; *i &lt;&lt; &quot;\n&quot;;
}
// do something with i
</pre>
<p>There is a fair amount going on here, which bears explanation.
Firstly, the definition of a list:</p>
<pre class="programlisting">
list&lt; string &gt; names;
</pre>
<p>This creates a <tt class="classname">list</tt> of <tt class=
"classname">string</tt>s called <tt class="varname">names</tt>. The
<tt class="constant">string</tt> argument is the template argument
to the list container class (it isn't called the Standard Template
Library for nothing!) and this can be any type which meets certain
requirements, which we will look at later.</p>
<p>Next we declare a <tt class="classname">string</tt>, and read it
from the standard input. As each <tt class="classname">string</tt>
is read, we add it to the list using <tt class=
"methodname">push_back()</tt>. It is important to note that the
string is copied into the list as a new value; each iteration round
the loop overwrites name, so this is desired behaviour. Also, note
that the list grows automatically every time a new element is added
to it; you don't have to allocate the memory yourself.</p>
<p>Next comes the really interesting part. We use an iterator to
loop through the list to get at each element. We create a new
iterator and initialise it to point at the beginning of the list,
using the <tt class="methodname">begin()</tt> member function of
the list itself. It is important that the iterator we create is for
a list of <tt class="classname">string</tt>s; any other iterator
just will not do. Next, we test to see if the current position of
the iterator is at the <tt class="methodname">end()</tt> of the
list. <tt class="methodname">list::end()</tt> is actually one past
the last element in the list, so when we reach this point, we know
there are no more elements, and in fact trying to dereference the
end of a container is a bad idea. Finally, we increment the
iterator using the familiar increment notation.</p>
<p>Inside the loop, we dereference the iterator and output the
contents to the console. Anyone familiar with pointer notation in C
and C++ will recognise this. All iterators for the standard library
containers support this usage for iterating over their elements and
dereferencing the current element.</p>
<p>If you think that the example above is a little verbose, don't
worry; we will be exploring a much better way of achieving the same
result later on. For now, a basic understanding of the iterators in
standard containers will suffice.</p>
<p>This verbosity can be alleviated using <tt class=
"literal">typedef</tt> to give a name to the type:</p>
<pre class="programlisting">
typedef list&lt; string &gt;::iterator output;
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e196" id="d0e196"></a>vector</h3>
</div>
<p>In the meantime, we will move on to the next sequence container,
vector. The first point of interest here is that the equivalent
code for reading the names from the console which we did using a
list previously, is almost identical when we replace list with
vector: (the differences are underlined)</p>
<pre class="programlisting">
<span class="underline">vector</span>&lt; string &gt; names;
string name;
while( cin &gt;&gt; name &amp;&amp; name != &quot;done&quot; )
{
  names.push_back( name );
}
<span class="underline">vector</span>&lt; string &gt;::iterator i;
for( i = names.begin(); i != names.end(); ++i )
{
  cout &lt;&lt; *i &lt;&lt; &quot;\n&quot;;
}
// do something with i
</pre>
<p>As it turns out, vector's iterators are more sophisticated than
list's, but for the main examples, they are identical in
appearance. We can increment (and decrement, by the way: --i in
this example) the iterator, dereference it, and copy or compare it
to another iterator of the same type.</p>
<p>What vector also allows us to do, that list does not, is access
the container at any valid location. A side effect of this is that
we can increment and decrement a vector's iterator by more than
one, provided the result is still valid. A demonstration:</p>
<pre class="programlisting">
vector&lt; string &gt; names;
// ... 
string temp = names[ 10 ];
vector&lt; string &gt;::iterator a_name = 
              names.begin() + 10;
string temp2 = *a_name;
</pre>
<p>Here, both <tt class="varname">temp</tt> and <tt class=
"varname">temp2</tt> now have copies of element 10 in the vector,
assuming that there are at least 11 names contained there (element
0 is the first element). If there were fewer than 11, then both
string assignments would be invoking undefined behaviour.</p>
<p>This behaviour is allowed because vector provides Random Access.
Why the new font? Random Access is a concept, as opposed to a
language feature. The point is that vector uses Random Access
Iterators [<a href="#Austern1998">Austern1998</a>], whereas list
uses Bidirectional Iterators. We have already seen the main
difference between them, but it is important to note that Random
Access is a refinement of Bidirectional, i.e. it implies
Bidirectional behaviour as well. There are several iterator types,
but suffice to say that all the standard containers provide
Bidirectional iterators.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e228" id="d0e228"></a>deque</h3>
</div>
<p>The final sequence container is deque, pronounced &quot;deck&quot;. In
order to understand the benefits afforded by deque, we need to look
at what makes vector and list so different from each other in terms
of their management of memory.</p>
<p>vector provides an array of elements, which occupy a contiguous
area of memory<sup>[<a name="d0e235" href="#ftn.d0e235" id=
"d0e235">2</a>]</sup>. This has implications for two important
things. Firstly, inserting an element anywhere but at the end of a
vector requires that all existing elements which follow it must be
shifted in memory. Secondly, when a vector needs more memory and is
unable to satisfy that need from its own memory block, a new block
needs to be allocated and all existing elements copied into it.
Clearly these are expensive operations for large containers.</p>
<p>list, on the other hand, is at the other extreme. It's elements
need not be in a single block of memory (list actually doesn't care
either way - it assumes they are not) but it must allocate space
for every element as it is added. Memory allocation is also a
relatively expensive operation (compared to, say, accessing an
element) and where a large number of elements is added to a list in
one go, an allocation needs to take place for each element.</p>
<p>So, vector supports direct access to any element in the sequence
(courtesy of Random Access), at the expense of what you might call
Random Insertion - inserting an element anywhere but at the end is
inefficient. list, in contrast, supports efficient Random Insertion
at the expense of Random Access.</p>
<p>deque provides the middle ground. There is no guarantee that a
deque's elements occupy contiguous memory, but it does provide
Random Access to the elements. What this means in practice is that
you can insert or remove elements from either end of a deque, and
this will be efficient, because when new memory is required, there
is no need to copy all the existing elements into it. Insertion or
removal from anywhere in the middle is not guaranteed to be
efficient, however.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e248" id="d0e248"></a>In
Summary</h2>
</div>
<p>It is important that you choose carefully among the sequence
containers, depending on what your requirements are. As a simple
guide, you may find the following useful:</p>
<p>Use a list. Unless...</p>
<p>If you require contiguous elements, or need random access, use a
vector.</p>
<p>If you know in advance how many elements there will be, and/or
you are creating a collection from another collection, use a
vector.</p>
<p>If you want random access and the ability to insert or remove at
either end, use a deque.</p>
<p>(After [<a href="#Meyers1999">Meyers1999</a>])</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e266" id="d0e266"></a>The Cave of
Echoes: Functors</h2>
</div>
<p>Our next point of call is a cave which is larger than it looks,
and has all sorts of interesting hiding places where you may find
fascinating insights into the world of C++'s power and elegance.
We've only got time to look at the more conspicuous features for
now, but hopefully you'll get an impression of the treasures that
we miss on the way.</p>
<p>The main structure underpinning procedural programming was,
well, procedures, which in C, as in C++, are represented by
functions. Object Oriented programming is underpinned by the
object, which has methods - functions which operate on the object
rather than on parameters passed to it.</p>
<p>Generic programming is underpinned by the Functor or Function
Object, which is something in between, or rather, has elements of
both.</p>
<p>Consider the regular C++ function. It is a way of encapsulating
a process, of abstracting the &quot;how&quot; of an algorithm from the
&quot;what&quot;. OK, let's get specific about this. So far, we've been
printing the elements in a container<sup>[<a name="d0e277" href=
"#ftn.d0e277" id="d0e277">3</a>]</sup>:</p>
<pre class="programlisting">
void print_me( const string &amp; s )
{
  cout &lt;&lt; s &lt;&lt; &quot;\n&quot;;
}
list&lt; string &gt;::iterator i;
for( i = names.begin(); 
        i != names.end(); ++i )
{
  print_me( *i );
}
</pre>
<p>This is all very well, but what if the contained elements were
<span class="emphasis"><em>not</em></span> strings? A simple way to
solve that problem is to make <tt class="function">print_me</tt> a
template function:</p>
<pre class="programlisting">
template&lt; typename T &gt;
void print_me( const T &amp; s )
{
  cout &lt;&lt; s &lt;&lt; &quot;\n&quot;;
}
</pre>
<p>Now it can be used for any type which defines <tt class=
"methodname">operator&lt;&lt;()</tt> for standard output streams.
So far, so good. What happens, though, if we want to output our
elements to a stream other than the console? Our first attempt
might be to overload <tt class="function">print_me</tt> to take an
ostream argument, but we can do better than that.</p>
<pre class="programlisting">
class print_me
{
public:
  explicit print_me( ostream &amp; s )
    : strm( s )
    { }
  void operator()( const string &amp; rhs )
  {
    strm &lt;&lt; rhs &lt;&lt; &quot;\n&quot;;
  }
  
private:
  ostream &amp; strm;
};
</pre>
<p>A slight return to allowing only strings to be printed here
aside, this is a Function Object, or Functor. It is so called
because it defines the function-call operator: <tt class=
"methodname">operator()</tt>. Just as you can overload operators +,
-, *, etc, you can provide your own versions of this operator so
that an instance of your class can appear in an expression
<span class="emphasis"><em>as if it were</em></span> a
function:</p>
<pre class="programlisting">
print_me printer( cout );
printer( &quot;Steve&quot; );
</pre>
<p>The constructor associates the <tt class=
"classname">print_me</tt> object with the required state (the
output stream object), and so, &quot;calling&quot; the functor with a
matching argument (or, as here, one which is substitutable for the
direct match [<a href="#Henney2000a">Henney2000a</a>], [<a href=
"#Henney2000b">Henney2000b</a>]).</p>
<p>This could then be used inside the loop as before:</p>
<pre class="programlisting">
list&lt; string &gt;::iterator i;
for( i = names.begin(); 
      i != names.end(); ++i )
{
  printer( *i );
}
</pre>
<p>When we wanted the function to be able to handle any type for
which the stream insertion operator was defined, we made it a
template function. However, there is a snag with making the
<tt class="classname">print_me</tt> class a template; consider how
it would be used:</p>
<pre class="programlisting">
list&lt; string &gt;::iterator i;
for( i = names.begin(); 
          i != names.end(); ++i )
{
  printer&lt; string &gt;( *i );
}
</pre>
<p>This is hardly ideal. Since the compiler &quot;knows&quot; the type of
<tt class="varname">*i</tt> (it's an iterator for a container of
strings, and so points to a string element), we would like very
much for the compiler to deduce the template type of the <tt class=
"classname">print_me</tt> object. The compiler can't do this at
all; it can only deduce template arguments for functions because
you pass actual arguments to the function itself. And here lies our
clue: we can make the member function <tt class=
"methodname">operator()</tt> a template, thus allowing the compiler
to deduce the template arguments from our actual parameters to
it:</p>
<pre class="programlisting">
class print_me
{
public:
  explicit print_me(ostream &amp; s)
    : strm( s )
    { }
  template&lt; typename T &gt;
  void operator() (const T &amp; rhs)
  {
    strm &lt;&lt; rhs &lt;&lt; &quot;\n&quot;;
  }
  
private:
  ostream &amp; strm;
};
</pre>
<p>And there you have it - the final version of our functor. Now we
can use it as we would really have used a function. This turns out
to be surprisingly expressive and powerful, especially when we come
to look at the standard algorithms. We can use it to compose
operations together. For example, consider our container of names.
Supposing we want to find an element that matches <span class=
"emphasis"><em>either</em></span> this name or that one. We might
define a functor like this:</p>
<pre class="programlisting">
struct name_is
{
  explicit name_is(const string &amp; n)
    : name(n)
    { }
  bool operator()
        (const string &amp; rhs) const
  {
    return name == rhs;
  }
  const string &amp; name;
};
name_is name1( &quot;Steve&quot; );
name_is name2( &quot;David&quot; );
vector&lt; string &gt;::const_iterator i;
for( i = names.begin(); i != names.end(); ++i )
{
  if( name1( *i ) || name2( *i ) )
  {
    break;
  }
}
// do something with i
</pre>
<p>Here we have two objects of type <span class=
"type">name_is</span>, each with different stored state. Using
functions alone, this simply cannot be done: such local state would
have to be a global or static variable. The only alternative would
be to pass in the second name every time.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e368" id="d0e368"></a>Source of the
River: Algorithms</h2>
</div>
<p>As we've already seen, we can use the Standard Library
containers to store elements of (nearly) any type, and we can get
access to those elements, read them, write to them and so on. For
anyone who has had to implement such container &quot;classes&quot; in C, or
even in C++, just having them provided has obvious benefits,
especially since they inter-operate so well.</p>
<p>The STL portion of the C++ Standard Library is much more than
the container classes, however. In fact they represent only a small
part of the facilities provided. It is here, with algorithms, that
we can combine the simple concepts of Iterators (and through them,
the facilities of the Containers) and the power of functors to
great effect.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e375" id="d0e375"></a>More On
Containers And Iterators</h2>
</div>
<p>We have already seen that the container classes provide
iterators for themselves via <tt class="methodname">begin()</tt>
and <tt class="methodname">end()</tt> member functions, which give
iterators to the start and one past the end of the elements. Since
iterators from any of the containers, associative and sequence
alike, can be incremented to get the next element, every container
looks like a sequence when viewed exclusively with iterators. Thus
you have the concept of an Iterator Range. For an entire container,
such a range is expressed as a half open interval [<a href=
"#Austern1998">Austern1998</a>]:</p>
<pre class="programlisting">
[begin, end)
</pre>
<p>This notation means that the sequence starts with <tt class=
"varname">begin</tt>, and that <tt class="varname">end</tt> can be
reached by incrementing <tt class="varname">begin</tt>. <tt class=
"varname">end</tt> itself is one after the last element.
Specifically, the range includes <tt class="varname">begin</tt>,
but not <tt class="varname">end</tt>.</p>
<p>We have already touched on Iterator Concepts like Random Access
and Bidirectional. Some algorithms expect the iterators to conform
to a particular concept like this; most require only an Input
Iterator (one which can be read from and incremented) but some,
like <tt class="methodname">sort()</tt>, require Random Access
Iterators<sup>[<a name="d0e416" href="#ftn.d0e416" id=
"d0e416">4</a>]</sup>. Remembering that these restrictions are
minimum requirements is important; a Random Access Iterator makes a
perfectly good Input Iterator, as does a Bidirectional one.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e420" id="d0e420"></a>An
Illustration</h2>
</div>
<p>Way back when we first looked at containers, we saw a somewhat
lengthy piece of code that iterated over a container of strings,
printing each one to the console. It was noted at the time we would
see a better way to achieve this, and here it is. First, a reminder
of the original:</p>
<pre class="programlisting">
list&lt; string &gt;::iterator i;
for( i = names.begin(); i != names.end(); ++i )
{
  cout &lt;&lt; *i &lt;&lt; &quot;\n&quot;;
}
</pre>
<p>We can enlist the help of the <tt class=
"classname">print_me</tt> object we defined earlier to reduce this
to a single line of code:</p>
<pre class="programlisting">
for_each(names.begin(), names.end(), print_me(cout));
</pre>
<p>There is yet a better way, involving a different beast - an
<tt class="classname">ostream_iterator</tt> - which does away with
the need for our functor, but that's beyond the current scope.</p>
<p>Here, then, we use a Standard Algorithm, <tt class=
"methodname">for_each()</tt>, which takes an iterator range, and an
operation to perform on each element. Note also that the <tt class=
"classname">print_me</tt> object is an <span class=
"emphasis"><em>anonymous temporary</em></span>, which means we have
no need to construct it separately, and it automatically goes out
of scope at the end of the &quot;loop&quot;. We have lost none of the
notational convenience of the templated member function - it is
still done by the compiler as required, but we have lost all the
verbosity of declaring our iterators in the previous cumbersome
for-loop.</p>
<p>The <tt class="classname">print_me</tt> functor is an example of
a Unary Function. It takes a single argument and can be called like
a regular C++ function. Correspondingly, there is a concept of
Binary Function, which takes two arguments. There are also special
types of Unary and Binary functions called Predicates which differ
in that they return a boolean value.</p>
<p>Our name_is functor is a Unary Predicate (takes one argument and
returns a bool). We could use it with for_each(), but for_each()
doesn't use the return value from the functor it uses. If we want
to find an element such as we attempted previously we need a new
algorithm:</p>
<p>Our <tt class="classname">name_is</tt> functor is a Unary
Predicate (takes one argument and returns a <span class=
"type">bool</span>). We could use it with <tt class=
"methodname">for_each()</tt>, but <tt class=
"methodname">for_each()</tt> doesn't use the return value from the
functor it uses. If we want to find an element such as we attempted
previously we need a new algorithm:</p>
<pre class="programlisting">
  find_if( names.begin(), names.end(),
              name_is(&quot;Steve&quot;));
</pre>
<p>This algorithm returns the iterator where the requested name is
found (specifically the point at which the predicate <tt class=
"classname">name_is</tt> returned true). If the object was not
found, then <tt class="methodname">names.end()</tt> is returned. In
this case, we could have used the simpler <tt class=
"methodname">find()</tt> algorithm, for which you need not provide
a predicate, only the value to find. Consider:</p>
<pre class="programlisting">
list&lt; string &gt; names;
// initialise names...
list&lt;string&gt;::iterator i = find(names.begin(), names.end(),&quot;Steve&quot;);
if( i != names.end())
{
  *i = &quot;Stephen&quot;;
}
</pre>
<p>This business of passing in the iterator range, rather than the
container itself, provides great flexibility in the way the
algorithms operate. In general, algorithms that search for an
element, or that change elements in some way, return an iterator
into the container, which can then be used as part of the iterator
range for another algorithm. Importantly, it is not necessary that
[Begin, End) is the complete range of the container.</p>
<p>In our quick look at functors, we ended with a way of using two
functors with different state, for the purpose of finding one or
the other in the container. Before we leave the caves altogether,
let's have a quick look into one of the darkest corners...</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e494" id="d0e494"></a>The Witches'
Broth: Adaptors and Adaptable Function Objects</h2>
</div>
<p>Different aspects of the STL are designed to work together, for
example, the containers and algorithms are glued together using
iterators, and the algorithms themselves can be customised with
your own code using functors.</p>
<p>The C++ Standard Library provides several functors for your use,
ones which perform simple, but vital operations, such as adding two
elements together (requiring only that <tt class=
"methodname">operator+()</tt> is defined for that type), or
determining if one is the same as another.</p>
<p>One of the simplest standard functors is called <tt class=
"classname">equal_to</tt> and does exactly that. Here is <tt class=
"classname">equal_to</tt> in action:</p>
<pre class="programlisting">
list&lt; string &gt; names;
find_if(names.begin(), names.end(),   
  bind1st(equal_to&lt;string&gt;(),&quot;Steve&quot;));
</pre>
<p>So what is this bind1st thing?</p>
<p><tt class="classname">bind1st</tt> is an Adaptor; it takes one
functor and &quot;adapts&quot; it into a different kind of functor. In
particular, it adapts binary functors into unary functors by
storing the first argument.</p>
<p>This code snippet becomes more clear when you understand that
<tt class="classname">equal_to</tt> is a Binary Predicate. It takes
two parameters of, in this case, <tt class="classname">string</tt>,
and determines if the left one is the same as the right, and
returns the result as a <span class="type">bool</span>.
Specifically, and this is the difference between <tt class=
"classname">equal_to</tt> and <tt class="classname">name_is</tt>,
you cannot construct an <tt class="classname">equal_to</tt> object
with the object to be tested; you must pass both arguments to the
function call operator.</p>
<p>Instead, you call <tt class="classname">bind1st</tt> with your
first parameter and a functor. <tt class="classname">bind1st</tt>
is itself a Unary Function<sup>[<a name="d0e549" href="#ftn.d0e549"
id="d0e549">5</a>]</sup>. What we're trying to achieve is to test
each element of the container against the string &quot;Steve&quot;, and
<tt class="classname">bind1st</tt> allows us to do that. The
algorithm <tt class="function">find_if</tt> will call <tt class=
"classname">bind1st</tt> (a Unary Function) with each element in
its range, and <tt class="classname">bind1st</tt> in turn will pass
that on to <tt class="classname">equal_to</tt>, along with the
<tt class="classname">string</tt> &quot;Steve&quot;.</p>
<p>This magic is possible because <tt class=
"classname">equal_to</tt> is an Adaptable Predicate. This grand
sounding title means nothing more (nor less) than <tt class=
"classname">equal_to</tt> has internal public <tt class=
"literal">typedef</tt>s for its arguments and return type. In
practice, it means that it publicly inherits from the conveniently
provided <tt class="classname">binary_function</tt> class, which
provides those <tt class="literal">typedef</tt>s according to
template parameters you provide. It looks something like this:</p>
<pre class="programlisting">
template&lt; typename FirstArgument,
        typename SecondArgument,
         typename ReturnType &gt;
struct binary_function
{
  typedef FirstArgument 
             first_argument_type;
  typedef SecondArgument 
              second_argument_type;
  typedef ReturnType   return_type;
};
template&lt; typename T &gt;
class equal_to : 
  public binary_function&lt; T, T, bool &gt;
{
public:
  bool operator()(const T &amp; l,const T &amp; r)
  {
    return l == r;
  }
};
</pre>
<p>Now, of course, <tt class="classname">equal_to</tt> has
<tt class="literal">typedef</tt>s identifying its argument and
return types. <tt class="classname">bind1st</tt>, along with all
the other adaptors, require these <tt class="literal">typedef</tt>s
so it can construct a function call from the various parts. There
is another base class for Unary Functions, called, oddly,
<tt class="classname">unary_function</tt>, which has only two
<tt class="literal">typedef</tt>s. So far we've discovered that we
can combine function objects with each other using adaptors to
provide functionality not achievable directly. However, when we
first looked at functors, we wanted to be able to search for one of
two names in our list of names, and for that we need to extend the
STL, because it only provides part of what we need. Before we leave
we'll take a quick look behind the scenes of what makes the STL
tick.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e610" id="d0e610"></a>Under The
Ropes</h2>
</div>
<p>Our goal is to be able to search a container of strings for
<span class="emphasis"><em>either</em></span> this name or that
one, as we did with our own <tt class="classname">name_is</tt>
functor:</p>
<pre class="programlisting">
name_is name1(&quot;Steve&quot;);
name_is name2(&quot;David&quot;);
vector&lt; string &gt;::const_iterator i;
for( i = names.begin();
      i != names.end(); ++i )
{
  if( name1(*i) || name2(*i))
  {
    break;
  }
}
// do something with i
</pre>
<p>Looking through the facilities provided by the C++ Standard
Library, we find there is a Binary Predicate called <tt class=
"classname">logical_or</tt>, which looks like it suits our purpose.
Perhaps we could put it to use like this:</p>
<pre class="programlisting">
find_if(names.begin(), names.end(),
  logical_or&lt; bool &gt;( 
    bind1st(equal_to&lt; string &gt;(), 
           &quot;Steve&quot; ),
    bind1st(equal_to&lt; string &gt;(),
           &quot;David&quot;)));
</pre>
<p>Alas this isn't possible; <tt class="classname">logical_or</tt>
expects to be able to apply the <tt class="literal">||</tt>
operator to its arguments, which means they must be (convertible
to) <span class="type">bool</span><sup>[<a name="d0e640" href=
"#ftn.d0e640" id="d0e640">6</a>]</sup>. What we need is to be able
to compose <tt class="classname">logical_or</tt>, and the two bound
<tt class="classname">equal_to</tt>s into a single expression.</p>
<p>In an expression like <tt class="literal">x || y</tt>,
<tt class="varname">x</tt> and <tt class="varname">y</tt> are
operands, and <tt class="literal">||</tt> is the operator. So, we
have a left operand, a right operand and an operator to consider.
Since we want to make this &quot;composer&quot; object as general as we can,
we can use templates to represent these concepts, and start from
there.</p>
<p>We'll start with an object called <tt class=
"classname">binary_compose</tt>. It will be used to compose two
operands into a Binary Function, so this makes sense.</p>
<pre class="programlisting">
template&lt;   class Operator, 
      class XOperand, 
      class YOperand &gt; 
struct binary_compose 
{
  Operator op;
  Xoperand x;
  Yoperand y;
};
</pre>
<p>We need a constructor in order to store these things so that the
function call operator can call them:</p>
<pre class="programlisting">
binary_compose( const Operator &amp; f, const XOperand &amp; g, const YOperand &amp; h ) 
  : op(f), x(g), y(h) 
  { }
</pre>
<p>Next we need a function call operator; this is a functor which
can be used from an algorithm such as <tt class=
"classname">find_if()</tt>. How do we define this? The return type
needs to be the return type of the operator, and the argument to
the (unary) function call operator needs to be the same as that for
<tt class="classname">bind1st</tt> (which will be the unary
function in our case). This is where the nested <tt class=
"literal">typedef</tt>s of <tt class=
"classname">unary_function</tt> come in.</p>
<pre class="programlisting">
typename Operator::result_type 
  operator()(const typename 
         XOperand::argument_type &amp; l )
{
}
</pre>
<p>Note the use of the keyword typename here. Without it, the
compiler might think that <i class=
"parameter"><tt>argument_type</tt></i> and <span class=
"returnvalue">result_type</span> were static members of class
<tt class="classname">XOperand</tt>, since it doesn't know the type
of <tt class="classname">XOperand</tt> due to it being a template
parameter. The <tt class="literal">typename</tt> keyword is
<span class="bold"><b>required</b></span> when a name of a type is
dependent on a template parameter [<a href=
"#Stroustrup2000">Stroustrup2000</a>].</p>
<p>Anyway we're at the last hurdle. What should the implementation
of <tt class="methodname">operator()</tt> be? Well, we need to call
the operator; this suggests</p>
<pre class="programlisting">
  op( ?, ? ); 
</pre>
<p>as a starting point. Focusing on using <tt class=
"classname">logical_or()</tt> here, we know that it takes two
parameters, both <span class="type">bool</span>. We want to be able
to use <tt class="classname">equal_to()</tt>, which returns a
<span class="type">bool</span>. We will be using it via the adaptor
<tt class="classname">bind1st</tt>, but it turns out that makes no
difference. Simple:</p>
<pre class="programlisting">
typename Operator::result_type 
  operator()(const typename 
          XOperand::argument_type &amp; l)
{
  return op(x(l), y(l));
}
</pre>
<p>When we substitute what we know in this case will become the
various arguments, we have:</p>
<pre class="programlisting">
logical_or&lt;bool&gt; op;
bind1st x(equal_to&lt;string&gt;(), &quot;Steve&quot;);
bind1st y(equal_to&lt;string&gt;(), &quot;David&quot;);
op(x(l), y(l));
</pre>
<p>Remember <tt class="classname">bind1st</tt> is a function, not a
class, so this won't compile. So we have two objects, <tt class=
"function">x</tt> and <tt class="function">y</tt>, which are
functors, and are later called using the argument to this function
call operator. We do have to go one step further, however, because,
if you remember, the compiler won't be able to deduce our template
parameters, because <tt class="classname">binary_compose</tt> is a
class. Since we're storing objects of the template types, we can't
use a template member function as we did for our <tt class=
"classname">print_me</tt> functor, but we can use a helper template
function which returns a <tt class="classname">binary_compose</tt>
object of the right type. This may seem familiar - it's exactly
what <tt class="classname">bind1st</tt> is for <tt class=
"classname">binder1st</tt>.</p>
<pre class="programlisting">
template&lt;class Op, class XOp, class YOp&gt;
binary_compose&lt;Op, XOp, YOp&gt; compose2(
            Op op, XOp xop, YOp yop 
{
  return binary_compose&lt;Op, XOp, YOp&gt;
                    (op, xop, yop);
}
</pre>
<p>So now we can use this in an expression like:</p>
<pre class="programlisting">
find_if( names.begin(), names.end(),
  compose2(logical_or&lt;bool&gt;,
    bind1st(equal_to&lt;string&gt;, &quot;Steve&quot;),
    bind1st(equal_to&lt;string&gt;,&quot;David&quot;)));
</pre>
<p>Lastly, why <tt class="classname">binary_compose</tt> and
<tt class="classname">compose2</tt>? In the first case, we're
composing a binary operation (<span class=
"emphasis"><em>this</em></span> or <span class=
"emphasis"><em>that</em></span>); the binary doesn't relate to the
composed functions but the required result. <tt class=
"classname">compose2</tt> is so named because we want to &quot;compose
two&quot; operands though an operator functor.</p>
<p>However, I must own up. Both <tt class=
"classname">binary_compose</tt> and <tt class=
"classname">compose2</tt>, along with <tt class=
"classname">unary_compose</tt> and <tt class=
"classname">compose1</tt> for making a unary operation, are common
extensions to the STL. If your implementation doesn't have it, you
can use this one<sup>[<a name="d0e811" href="#ftn.d0e811" id=
"d0e811">7</a>]</sup>. <tt class="classname">compose1</tt> and
<tt class="classname">unary_compose</tt> are, as ever, left as an
exercise...</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e824" id="d0e824"></a>Back At The
Exit</h2>
</div>
<p>We have explored quite a lot of territory in our short trip,
which has taken a little longer than anticipated, but I hope it's
been worth it. There are many more strange and wonderful sights to
see, and detailed tours are given by such famous explorers as
[<a href="#Austern1998">Austern1998</a>], [<a href=
"#Stroustrup2000">Stroustrup2000</a>] and [<a href=
"#Josuttis1999">Josuttis1999</a>].</p>
<p>I hope, however, that these is enough material here for you to
be more sure-footed in your own explorations of the C++ Standard
Template Library, and that we've thrown a little light on some
possibly heretofore obscure elements and grottoes. The C++ Standard
Template Library was designed deliberately to be extended, as we
have with <tt class="classname">compose2</tt>, an this is its most
important aspect. The STL is a specification for designing generic
components, using iterators, adaptors, functors and algorithms,
whether provided as part of the Standard Library itself, or written
by yours truly. &quot;Using STL effectively means extending it&quot;
[<a href="#Austern1998">Austern1998</a>].</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e846" id=
"d0e846"></a>Acknowledgements</h2>
</div>
<p>A big &quot;Hurrah!&quot; to Nigel Dickens and Kevlin Henney, who kindly
read drafts of this, and provided valuable feedback. Many thanks to
Alan Griffiths who graciously gave his permission for me to (ab)use
his metaphor from &quot;<span class="emphasis"><em>Here Be
Dragons</em></span>&quot;. More thanks to Kevlin Henney, who first
inspired the idea of poking around in dark corners to discover
secrets, (I'm sure I could have made better use of it - next time
maybe!) and for provided me with an advance copy of his
&quot;<span class="emphasis"><em>The Miseducation of C++</em></span>&quot;
article for Application Development Advisor [<a href=
"#Henney2001">Henney2001</a>], plus some articles yet to be
published. I've not referred to them directly, but they provided
valuable background. Special Thanks to Nigel Dickens and Phil Hibbs
for sparking and encouraging my interest in the STL in the first
place. Without whom, etc..</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e860" id=
"d0e860"></a>Bibliography</h2>
</div>
<div class="bibliomixed"><a name="Austern1998" id=
"Austern1998"></a>
<p class="bibliomixed">[Austern1998] Matthew H Austern,
<span class="citetitle"><i class="citetitle">Generic Programming
And The STL</i></span>, Addison Wesley 1998</p>
</div>
<div class="bibliomixed"><a name="Boost2001" id="Boost2001"></a>
<p class="bibliomixed">[Boost2001] <span class=
"bibliomisc"><a href="http://www.boost.org" target=
"_top">www.boost.org</a></span></p>
</div>
<div class="bibliomixed"><a name="Griffiths2000" id=
"Griffiths2000"></a>
<p class="bibliomixed">[Griffiths2000] Alan Griffiths, Here Be
Dragons, <span class="citetitle"><i class="citetitle">Overload
40</i></span>, 2000</p>
</div>
<div class="bibliomixed"><a name="Henney2000a" id=
"Henney2000a"></a>
<p class="bibliomixed">[Henney2000a] Kevlin Henney, From Mechanism
To Method: Substitutability, <span class="citetitle"><i class=
"citetitle">C++Report</i></span> 12(5), May 2000, Overload 39,
September 2000</p>
</div>
<div class="bibliomixed"><a name="Henney2000b" id=
"Henney2000b"></a>
<p class="bibliomixed">[Henney2000b] Kevlin Henney, From Mechanism
To Method: Valued Conversions, <span class="citetitle"><i class=
"citetitle">C++Report</i></span> 12(7), July 2000</p>
</div>
<div class="bibliomixed"><a name="Henney2001" id="Henney2001"></a>
<p class="bibliomixed">[Henney2001] Kevlin Henney, Effective C++:
The Miseducation of C++, <span class="citetitle"><i class=
"citetitle">Application Development Advisor</i></span>, April
2001</p>
</div>
<div class="bibliomixed"><a name="ISO1998" id="ISO1998"></a>
<p class="bibliomixed">[ISO1998] <span class="citetitle"><i class=
"citetitle">International Standard: Programming Language -
C++</i></span>, ISO/IEC 14882:1998(E), 1998</p>
</div>
<div class="bibliomixed"><a name="Josuttis1999" id=
"Josuttis1999"></a>
<p class="bibliomixed">[Josuttis1999] Nicolai Josuttis,
<span class="citetitle"><i class="citetitle">The C++ Standard
Library - A Tutorial and Reference</i></span>, Addison Wesley
1999</p>
</div>
<div class="bibliomixed"><a name="Koenig-1997" id=
"Koenig-1997"></a>
<p class="bibliomixed">[Koenig-1997] Andrew Koenig, Barbara Moo,
<span class="citetitle"><i class="citetitle">Ruminations on
C++</i></span>, Addison Wesley, 1997</p>
</div>
<div class="bibliomixed"><a name="Meyers1999" id="Meyers1999"></a>
<p class="bibliomixed">[Meyers1999] Scott Meyers, <span class=
"citetitle"><i class="citetitle">Effective C++ CD</i></span>,
Addison Wesley 1999</p>
</div>
<div class="bibliomixed"><a name="Stroustrup2000" id=
"Stroustrup2000"></a>
<p class="bibliomixed">[Stroustrup2000] Bjarne Stroustrup,
<span class="citetitle"><i class="citetitle">The C++ Programming
Language</i></span>, Special Edition, Addison Wesley 2000</p>
</div>
<div class="bibliomixed"><a name="STLport2000" id=
"STLport2000"></a>
<p class="bibliomixed">[STLport2000] <span class=
"bibliomisc"><a href="http://www.stlport.org" target=
"_top">www.stlport.org</a></span> (various source files,
<span class="bibliomisc"><tt class=
"classname">binary_compose</tt></span> in particular)</p>
</div>
</div>
<div class="footnotes"><br>
<hr class="c3" width="100">
<div class="footnote">
<p><sup>[<a name="ftn.d0e133" href="#d0e133" id=
"ftn.d0e133">1</a>]</sup> Please note that all of the code examples
here assume &quot;<tt class="literal">using namespace std;</tt>&quot; for
brevity</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e235" href="#d0e235" id=
"ftn.d0e235">2</a>]</sup> This is not actually guaranteed by the
C++ Standard at the time of writing, but it is certainly intended.
For those interested, vector probably couldn't meet its complexity
requirements without this feature. It will be in the first round of
corrections to the Standard [<a href="#ISO1998">ISO1998</a>].</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e277" href="#d0e277" id=
"ftn.d0e277">3</a>]</sup> [<a href="#Koenig-1997">Koenig-1997</a>]
uses a similar example for a rationale of &quot;why C++, and not C,
then?&quot;</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e416" href="#d0e416" id=
"ftn.d0e416">4</a>]</sup> Note that list provides its own sort(),
since it can't use the standard algorithm version</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e549" href="#d0e549" id=
"ftn.d0e549">5</a>]</sup> bind1st is actually a helper function
which deduces the templates for the adaptor itself (called
binder1st); recall that the compiler cannot deduce the template
parameters for a class. We will refer to bind1st as the adaptor,
however to avoid clouding the issue!</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e640" href="#d0e640" id=
"ftn.d0e640">6</a>]</sup> Or overload the || operator. For why this
is not a good idea, see item 7 in the &quot;More...&quot; section of
[<a href="#Meyers1999">Meyers1999</a>]</p>
</div>
<div class="footnote">
<p><sup>[<a name="ftn.d0e811" href="#d0e811" id=
"ftn.d0e811">7</a>]</sup> Or look at the much more sophisticated
adaptors and composers at [<a href="#Boost2001">Boost2001</a>]</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
