Templates and the Standard Template library
After Step 4 you will know, how function templates and class templates can be used to take advantage of the large STL library (Standard Template Library), which is included with all development tools for C++. By implementing an own “container”-application, you’ve got a deeper knowledge of how container objects facilitates a general use of the STL’s container classes. At the end of the step you have also got a brief orientation on storing, searching and sorting of data using the STL’s generic functions. This includes a presentation of the library’s iterators.
The “Lessons” in this last step is devoted to looking into the way C++ generalize behaviors by encapsulating them, in support of maintenance and reuse of software. Encapsulation is an important OO concepts, which hides an object’s actual implementation, in order to avoid (code) dependencies between classes. We continue by examining how function templates and class templates could help us to handle our objects in a general and independent way, using a surrounding “container object”. We then look more closely at some of STL’s container classes and show how they interact with generic algorithms (function templates adapted for container classes) and function objects. Finally we will analyze functionality for searching, storing and retrieving items in a container, by studying some program examples with vector and map. In conjunction with this is also studied the role of an iterator, and what demands are placed on different iterator types in varying contexts.
The theory, to study for the last course step, is to be found in the course book’s Ch. 16 (Templates) and Ch. 21 (Introducing the Standard Template Library).
Concepts that are covered:
- Encapsulation – objects as class members
- Container objects
- Function templates and class templates
- Comparison between some container classes
- Some examples with vector and map
- How to use generic functions
- Generally and specifically for iterators
- How to use function objects