Standard Template Library - An Introduction

One of the later editions to the C++ standard is the Standard Template Library (STL). The STL is a set of abstract datatypes, functions, and algorithms designed to handle user-specified datatypes. Each of the abstract datatypes also contains useful functions, including overloaded operators, to access them. The spirit of the Standard Template Library is the idea of generic programming - the implementation of algorithms or data structures without being dependent on the type of data being handled. For instance, you can use the STL vector container to store a vector (think of it as a resizable array) of any object you desire. In C and non-STL C++, you can use arrays to get a similar feature, but arrays are limited to a single type of data structure. Moreover, the STL provides some nice features such as handling memory for you (no memory leaks), and it is also safer (no buffer overflow issues when using vectors or similar data structures).

Note: If you don't know what templates are, you might want to read the templates tutorial for an overview. Although you don't need to know all the implementation details, you will need to know how to use templates to take advantage of the STL.

The STL offers the programmer several advantages in addition to safety and memory management. First, having the predefined types simplifies program design; no longer does a programmer have to write his own class to handle vectors, queues, lists, or associative arrays. Second, it offers powerful type-independent algorithms, including the obvious sorting and searching. The STL does so at very low cost to program performance, no more than any other templated class or function, and it's less likely that using a library function will lead to bugs than using your own code.

Finally, though not part of the STL, the standard library includes a string class, which will be covered in this set of tutorials. For those of you who have used the C string.h functions, it is a welcome relief to have access to a simplified string manipulation interface. The datatypes and functions in the STL are not limited to strings, but include vectors, linked lists, queues, stacks, as well as sorting, searching, numeric, permutation, and sequence operations.

The scope of the STL is wide. This tutorial will cover various data structures ("container classes") available, the use of iterators to access those data structures, templated algorithms, and a comparison between the various container classes provided by the STL.

Next: STL Vectors