Location: [CPSC 333] [Listing by Topic] [Listing by Date] [Previous Topic] [Next Topic] Standard Template Library
This material was covered during lectures on March 21, 1997.
The Standard Template Library (``STL'') is a general-purpose C++ library of ``generic algorithms'' and data structures. Its goals include making programmers more productive, because it includes many different components that can be plugged together and used in applications, and it provides an organization that can be applied to different programming problems.
The library (as described, for the most part, by the report of Stepanov and Lee referred to below) has been included in the ANSI/ISO C++ draft standard, so it's to be expected that it will be included with C++ in the future. However, it relies on some newer features (involving templates) that present C++ compilers don't all support yet, so you may have some difficulty using the library for the next while. For example, STL occasionally provides ``defaults'' for parameter values, and current compilers might not support this yet.
The primary reference (used to prepare these notes) for the Standard Template Library is the following book.
D. R. Musser and A. Saini
STL Tutorial and Reference Guide
Addison Wesley, 1996
This contains more details about each of the components described below, as well as several example programs, and a reference guide.
A shorter introduction is given in Chapter 2 of the next reference.
G. F. Rogers
Framework-Based Software Development in C++
Prentice Hall, 1997
Designers of the Standard Template Library have considered and (to a large part) overcome problems that had been recognized for packages of reusable components in the past:
Since C++ is an extremely widely used programming language, and since the Standard Template Library will be part of this language's standard, it's to be expected that this library will available for all platforms and will be supported by all (future) C++ compilers, so that it can be used in portable programs.
The designers of the Standard Template Library were careful to ensure
that the classes they included could be implemented efficiently, and
included ``performance guarantees'' for the interface requirements for
the library, by specifying bounds on the maximum time that each
function in the interface can take. ``Big-Oh'' notation is used for
these guaranteed. That is, if an interface function can accept some
sort of structure of size n as input, then the specification of
each interface function in the library includes the promise that its
worst case running in time is in O(T(n)), where T(n) is
usually either 1, log n, n, n log n, or
(rarely) n2.
Since the time used by the function will depend on the particular
implementation of the library that's being used, as well (if measured
in real units of time, like seconds) on things that are out of the
control of the developers and implementers of the library (such as the
system load at the time when the function is used), this is just about
the most precise promise of this kind that can be made; it's a far
better kind of performance guarantee than many other libraries
provide.
Finally, the interface chosen for classes in the library is reasonably
simple, highly standardized, and consistently used. The components of
the library will be discussed in more detail, below. However, it
should be noted at this point that
One additional point: The library relies on, and serves as a good
example for, generic programming. That is, it includes data
structures with standardized (and common) interfaces, including
standard ``access'' operations, along with algorithms that only
access data using these operations, so that it is possible to ``plug''
almost any data structure obtained using the library ``into''
almost any of the algorithms. One benefit of this is that it
would allow you to write one version of an algorithm - say, a
``quicksort'' algorithm - and then use it repeatedly with a
variety of different data structures, without having to make any of
the modifications to code for the algorithm (or, perhaps to recompile
it) that would be necessary if the algorithm had been designed (and
implemented) differently.
It's probably most important that the first few components described
below - ``containers,'' ``generic algorithms,'' and ``iterators,'' be
well understood. ``Allocators,'' ``adaptors,'' and (perhaps)
``function objects'' are probably less important.
As mentioned (repeatedly) below, additional online references at the
bottom of this page. The material included isn't supposed to be
comprehensive. Instead, it's hoped that it'll give you a general idea
of the design and components of the library quickly, before you go
onto the more detailed (and, sometimes, more technical) references
that follow it.
Containers in STL are objects that store collections of other
objects. There are two categories of STL container types: sequence
containers and sorted associative containers.
Sequence containers organize a collection of objects, all of the same
type T, into a strictly linear arrangement. The STL sequence
container types are as follows.
Sorted associative containers provide an ability for fast retrieval of
objects from a collection based on keys. The size of the collection
can vary at run time. STL has four sorted associative container types.
Generic algorithms are designed to be used with containers; all access
to data is performed using standard ``container access'' operations,
so that you can use containers to design a wide variety of data
structures, and then easily use the supplied generic algorithms with
almost all of the data structures you obtain. (See the more
detailed documentation, provided below, for limitations and additional
details.)
The Standard Template Library includes generic algorithms for a number
of common operations on data structures. They fall into four broad
categories: Nonmutating sequence algorithms operate on
containers without, in general, modifying the contents of the
container. Mutating sequence algorithms modify the contents of
the containers on which they operate. Sorting-related
algorithms include sorting and merging algorithms, binary search
algorithms, and set operations on sorted sequences. Finally, the
library includes a small collection of generalized numeric
algorithms. See the additional documentation for details about
each.
Iterators provide some of the ``standard container access''
mentioned above. They are pointer-like objects that generic algorithms
use to traverse the sequence of objects stored in a container. They
are an extremely important part of the design of the library, because
they serve as intermediaries between containers and generic
algorithms; you could think of them as the standard ``cables'' or
``connectors'' that you'd use to link STL ``containers'' to the
``generic algorithms'' that you want to operate on them.
Iterators in STL are classified into five categories. Roughly
speaking, input iterators support you to ``read'' the sequence
of objects in a container efficiently in one direction; output
iterators allow you to ``write'' or modify the objects efficiently,
again moving in one fixed direction. Forward iterators support
the operations of both ``input'' and ``output'' iterators (again,
moving in one direction through the container). Bidirectional
iterators support these operations when moving in both directions
through the sequence. Finally, certain STL data structures have
random access iterators as well. Again, see the additional
references for details.
In C++ a function object is an object of any class (or
struct) that overloads the function call operator,
operator().
Most STL generic algorithms, and some container classes, accept a
function object as a parameter. This makes it possible to vary their
computations in other ways besides those controlled by the iterators.
For example, the STL sort algorithm can be called with a
parameter, comp, that tells the sort algorithm how
two objects should be compared.
Passing a function object to an algorithm is similar to passing a
pointer to a function. However, it is possible to pass function objects
to algorithms at compile time, and it is also possible to increase
efficiency, ``by inlining the corresponding call.'' Thus, STL's use of
function objects instead of function pointers increases the likelihood
that generic algorithms can be used in many situations without losing
efficiency.
STL includes a small set of function object types that are commonly
used. Again, the additional references given below should be used to
find additional details.
Adaptors are STL components that can be used to change the
interface of another component. They are defined as template classes
that take a component type as a parameter.
STL provides container adaptors, iterator adaptors, and
function adaptors.
An example of a container adaptor is a
stack container adaptor, which can be applied to a
vector, list, or queue to produce a
container with the interface you'd expect for a stack, and with an
underlying implementation as a vector, list, or queue, respectively.
An example of an iterator adaptor is STL's
reverse_bidirectional_iterator, which can be applied to a
bidirectional iterator in which the (usual) traversal direction is
reversed.
Finally, an example of a (kind of) function adaptor is a
negator, which ``reverses the sense of predicate objects.''
Every STL container class uses an Allocator class to
encapsulate information about the memory model the program is
using. The default allocator class supplied with STL implementations
is generally sufficient, so this isn't something you need to be
concerned with immediately. However, one intended purpose of this is
apparently to provide a ``hook'' to an object-oriented data base,
allowing persistent objects to dealt with in a (reasonably)
transparent way.
The Standard Template Library is currently installed, and can
be found in the directory /home/packages/STL. This includes a
``frequently asked questions'' file, stl.faq, as well as
directories of example programs and ``include'' files.
Unfortunately, it doesn't currently seem to be very useful: The
examples and include files don't seem to be compatible (even though
the came from the same place). Thus, you can look through this
material in order to get a better idea of how the library ``should''
be used, but you shouldn't expect examples, or the code you write, to
compile correctly.
Jack Kirman
(at the Department of Computer Science,
Brown University) has written a
``modest STL tutorial.''
The latest version of this is also available as a
Postscript file, which you should be able to
view if you are viewing this page using Netscape on one of the
computer science department's machines running Unix.
Here at Calgary, Rob
Kremer has produced a set of web pages that give an
STL overview. You will almost certainly need to use Netscape, or
another powerful browser, in order to view these pages.
SGI has an impressive
online reference for the Standard Template Library, including
lists of the operations provided for each container.
Finally, a more detailed technical report of Stepanov and Lee is
available as a Postscript file that you can view
if your web browser is configured for this.
It may be possible for multiple copies of the above Postscript files
to be printed. Please let me know if you'd like this to be done,
before using laser printing access to print off individual copies of
these documents.
Location:
[CPSC 333]
[Listing by Topic]
[Listing by Date]
[Previous Topic]
[Next Topic]
Standard Template Library
Components
Containers
Sequence Containers
Sorted Associative
Containers
Generic Algorithms
Iterators
Function Objects
Adaptors
Allocators
Local Installation Information
Additional Online References
``A Modest Tutorial''
Rob Kremer's Introduction
SGI's Online Reference
Stepanov and Lee's Report
Request to CPSC 333 Students
Department of Computer Science
University of Calgary
Office: (403) 220-5073
Fax: (403) 284-4707
eberly@cpsc.ucalgary.ca