Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Standard Template Library (STL)

The STL consists of several kinds of components; the most important are containers, algorithms, and iterators. The main purpose of the STL was to establish a standard software library without loss of performance.

Why templates? The main advantage of templates is that they allow the use of types as parameters when creating classes. Developers therefore can create the desired class by specifying the proper parameters. In other words, data structures and functions will behave exactly the same, regardless of the data type. Sorting a collection of numbers or a collection of characters is done exactly the same: The language knows how to implement the proper method for each (number sort or character sort) by the parameter that was sent. This is hidden from the developer and allows for generic programming.

A container manages the storage and processing of data. You can think of it as an object that holds other objects. Containers are implemented by template classes to allow them to hold different kinds of data in an easy manner. The STL contains the most common containers that programmers find useful when handling common programming tasks.

Iterators provide methods to traverse the contents of a container and are used to access individual elements in a container. Similar to pointers, iterators can be incremented to point to the next element, and they can be dereferenced to obtain the value of the element to which it points. Iterators allow algorithms and containers to connect and create the bond that enables them to work together.

Algorithms allow functions to be performed on the containers. They are not container-specific or members of the containers; instead, they are standalone functions that are general in nature. The algorithms may be used on containers you create yourself or C++ arrays. Algorithms are represented by template functions.

Share ThisShare This

Informit Network