Software Design Using C++Introduction to the STLWhat is the STL?The Standard Template Library provides container classes for many data structures. Here is a partial list: vector, list, map, deque (double-ended queue), stack, queue, set, etc. Each such class provides typical functions for inserting a data item, removing an item, etc. The STL also provides algorithms, which are usually more involved than the container class functions. These algorithms can typically be used on any of the container classes. Examples of often-used algorithms are sort routines and search routines. Also, the STL uses iterators, which are sort of generalized pointers, that allow you to move through the items in a container.
Some practical matters need to be mentioned here. To use the STL, you need
to include the appropriate header and use the TemplatesSince the STL uses templates heavily, let's begin by looking at the topic of templates themselves. Too often in the past programmers have had to write nearly identical code, say to sort an array, simply because in one case they needed to sort an array of floats, in another case an array of ints, and in another case an array of objects. This is ridiculous! A template function (or generic function) allows one to write the code once and then use that function with various data types.
The first two examples below use such template functions. In the first one,
an array of integers is sorted and printed, whereas in the second one, an
array of objects is sorted and printed. The template functions for
sorting, swapping, and printing are exactly the same in these two programs.
Based on how each of these functions is called, the compiler creates the
appropriate specialization, or instance of the function.
Even in the same program, one could call the Template ExamplesPrintArray function
is set up to use a generic data type T as the component type for the array:
In the code for the
The
In the first example program, these assumptions are not a problem since the
program uses an array of ints, and these operators all work for ints. In the
second program, which uses an array of objects, the class has to be set up to
provide overloaded operators
that work with objects of the class. That is a bit of a nuisance, but once this
is done, the exact same The third example above shows how to set up a template class (also called a generic class). This program has a template-based class called ArrStackClass that provides an array implementation of stacks. The nice feature about the generic class is that it can be used to provide a stack of integers and a stack of characters, with no change to the class or its functions. This particular example creates one stack of each, both in the same program.
The
Then, when you go to create an object belonging to this class, you need to
specify what type gets used for T. For example, to create a stack
Optional Template ExamplesThe purpose of this section is to show you a few more advanced things that can be done with templates. However, this section is marked as optional since these features of templates are not used as often as the ones above.
In templat5.cpp a template class is set up that is parameterized by two types. This template class is named OrderedPair since objects of this class are ordered pairs of values, where the two values in a given pair do not have to be of the same type. The test program sets up and prints various ordered pairs. The class declaration itself is shown below. Follow the link to the example for the function definitions.
Finally, the optional example templat6.cpp contains a template function that displays the maximum of a pair of values. Both items in the pair must be of the same generic type T. The function returns the maximum in the function name, just to show that this can be done. An alternate way, of course, would be to return the maximum in a reference parameter. Here is the function definition:
Back to the main page for Software Design Using C++ |