These questions are of the rhetorical variety, by which I mean that
they have been made up and answered by the page author, rather than
collected from users. (This may even be taking liberties with the term
"rhetorical".) In any case, they form a short and probably inadequate
selection of some of the queries that might occur to a programmer upon
a first encounter with the STL, from one person's point of view.
- What is the STL?
- It's a sophisticated and powerful library of template classes and
template functions that implement many common data structures and
algorithms, and forms part of the C++ Standard Library.
- Who developed the STL?
- The STL was developed at Hewlett Packard by Alexander Stepanov,
with major contributions by David Musser and Meng Lee. It was deemed
by the C++ Standards Committee to be so important to the future of
C++ that the approval of the C++ Standard was delayed for several
years so that the STL could be incorporated into that Standard. A
version of the Standard that incorporated the STL was first approved
in the fall of 1998.
- Why should a C++ programmer be interested in the
STL?
- Because the STL embodies the concept of reusable software
components, and provides off-the-shelf solutions to a wide
variety of programming problems. It is also extensible, in
the sense that any programmer can write new software (containers and
algorithms, for example), that "fit in" to the STL and work with the
already-existing parts of the STL, provided the programmer follows
the appropriate design guidelines.
- What is the design philosophy of the STL?
- The STL exemplifies generic programming rather than
object-oriented programming, and derives its power and
flexibility from the use of templates, rather than inheritance and
polymorphism. It also avoids new and delete for
memory management in favor of allocators for storage
allocation and deallocation. The STL also provides performance
guarantees, i.e., its specification requires that the containers
and algorithms be implemented in such a way that a user can be
confident of optimal runtime performance independent of the STL
implementation being used.
- What are the major components of the STL?
- The containers and container adaptors (both are
objects that can hold other objects), the iterators
(generalized pointers that point at items in containers), and the
algorithms (that work on containers through iterators) will
be the most frequently used components of the STL.
- What are some other components of the STL?
- Other STL components include function objects (objects
of a class that defines operator()), and allocators
(which manage memory allocation and deallocation for containers).
Function objects are essential for effective use of the STL, but the
average user will be able to ignore allocators most of the time by
simply accepting the default allocator for each container used.
- How do you use the STL?
- By including the necessary header files to permit access to the
parts of the STL that you need, by declaring objects of the
appropriate container, iterator and function types, and then using
member functions and/or algorithms, as appropriate, to perform
whatever tasks your application requires. It is also generally
necessary to ensure that whatever objects you plan to put into your
container(s) are objects of classes that have a default constructor,
a copy constructor, and an overloaded operator=. In
addition, if you plan to sort or compare such container objects, the
corresponding classes must provide definitions for
operator== and operator<. Finally, since it is
often the case that different containers can be used in the same
problem situation, the user needs to be able to make an appropriate
choice for each occasion, and this choice will usually be based on
performance characteristics.
- What are some of the things a programmer should be
aware of when using the STL?
(Many of these may not make sense until you have actually tried to
use the STL.)
-
- Understand member functions size(),
max_size() and resize().
- Understand member functions begin(), end(),
rbegin() and rend().
- Understand the fact that a reference to a "range" of values
in STL generally means those values from (and including) the
first value in the range up to (but not including) the last value
in the range. Such a range can extend forward or backward,
depending on the kinds of iterators being used.
- Even though it may (sometimes) work, it is better
not use a loop like
for (i = c.begin(); i < c.end(); i++) ...
for processing all the elements of a container c; it is
preferable instead to use a loop like this:
for (i = c.begin(); i != c.end(); i++) ...
This second form is often necessary, in fact, since not all
container iterators support operator<.
- Remember that the container adaptors (stack,
queue, priority_queue) have no
iterators.
-
Know that any insert() member function applied to a
container may (or may not) invalidate iterators and references
already pointing at elements of that container. In particular:
- It is safest to assume that insert() applied to a deque
or a vector will invalidate any iterators or references to
elements of the deque or vector.
- On the other hand, insert() applied to any of
the other first-class containers (list,
map, multimap, set,
multiset) does not invalidate any of the
iterators or references to elements of the container, and
deletions from these containers invalidate only iterators or
references to the elements deleted.
- Remember that if you are using the STL for a given purpose in
a situation that requires (as a minimal requirement) that a
certain kind of iterator be used, a more powerful iterator may
also be used in that situation
- Remember that if you have the choice between using an STL
algorithm and using an STL container class method to accomplish
the same purpose, you should prefer the class method.
- Know that any container that is the target of an algorithm
must be large enough to hold all elements it will receive, since
it will not automatically be increased in size. Or, plan to use
an inserter.