4 THE CONCEPT OF THE C++ STANDARD TEMPLATE LIBRARY
C++ standard library including the STL part, but without the applications and the
extensions presented in this book.
1.1 Genericity of components
An interesting approach is not to emphasize inheritance and polymorphism, but
to provide containers and algorithms for all possible (including user-defined) data
types, provided that they satisfy a few preconditions. C++ templates constitute the
basis for this. Thus, the emphasis is not so much on object orientation but on generic
programming. This has the very important advantage that the number of different
container and algorithm types needed is drastically reduced – with concomitant type
security.
Let us illustrate this with a brief example. Let us assume that we want to find an
element of the int data type in a container of the vector type. For this, we need
a find() algorithm which searches the container. If we have n different containers
(list, set, ...), we need a separate algorithm for each container, which results in n
find() algorithms. We may want to find not only an int object, but an object of
an arbitrary data type out of m possible data types. This would raise the number of
find() algorithms to n · m. This observation will apply to k different algorithms,
so that we have to write a total of k · n · m algorithms.
The use of templates allows you to reduce the number m to 1. STL algorithms,
however, do not work directly with containers but with interface objects, that is,
iterators which access containers. Iterators are pointer-like objects which will be
explained in detail later. This reduces the necessary total to n + k instead of n · k, a
considerable saving.
An additional advantage is type security, since templates are already resolved at
compile time.
1.2 Abstract and implicit data types
Abstract data types encapsulate data and functions that work with this data. The data
is not visible to the user of an abstract data type, and access to data is exclusively
carried out by functions, also called methods. Thus, the abstract data type is specified
by the methods, not by the data. In C++, abstract data types are represented by
classes which present a tiny flaw: the data that represents the state of an object of this
abstract data type is visible (though not accessible) in the private part of the class
declaration for each program that takes cognizance of this class via #include. From
the standpoint of object orientation, ‘hiding’ the private data in an implementation
file would be more elegant.
Implicit data types can on the one hand be abstract data types themselves, on the
other hand they are used to implement abstract data types. In the latter case they
are not visible from the outside, thus the name ‘implicit.’ For example: an abstract
data type Stack allows depositing and removing of elements only from the ‘top.’ A