Table of Contents
Just as function templates allow us to write general patterns for functions, from which the compiler then generates multiple instances, so class templates allow us to write general patterns for classes.
Class templates are especially useful for “containers”, data structures that serve mainly as collections of other, smaller data types. In our spell checker, for example, we have used two different kinds of sets and two different kinds of ordered sequence. The code for the two sets was essentially the same -- only the data types of the contained data varied. The same is true if we compare the code for the two kinds of sequences.
Now, we certainly can get these different versions of the same container class by copying a file and editing it. But, as you've seen in the assignment at the beginning of the section, this approach can be tedious and error-prone. It also creates project management nightmares when a bug is fixed in one copy of an often-replicated container, and we must then find and fix all the copies.