Mastering STL Algorithms
WHAT’S IN THIS CHAPTER?
- What algorithms are
- What lambda expressions are
- What function objects are
- The details of the STL algorithms
- A larger example: auditing voter registrations
As Chapter 12 shows, the STL provides an impressive collection of generic data structures. Most libraries stop there. The STL, however, contains an additional assortment of generic algorithms that can, with some exceptions, be applied to elements from any container. Using these algorithms, you can find, sort, and process elements in containers, and perform a whole host of other operations. The beauty of the algorithms is that they are independent not only of the types of the underlying elements, but of the types of the containers on which they operate. Algorithms perform their work using only the iterator interfaces.
Many of the algorithms accept callbacks, which can be a function pointer or something that behaves like a function pointer, such as an object with an overloaded operator() or a C++11 inline lambda expression. Conveniently, the STL provides a set of classes that can be used to create callback objects for the algorithms. These callback objects are called function objects, or just functors.
OVERVIEW OF ALGORITHMS
The “magic” behind the algorithms is that they work on iterator intermediaries instead of on the containers themselves. In that way, they are not tied to specific container implementations. All the STL algorithms are implemented as function templates, ...