Using STL algorithms as building blocks
The Standard Template Library (STL) is a set of data types, containers, and algorithms included in the C++ standard. Even though we use containers on a daily basis, we tend to underuse the STL algorithms.
It's easy to forget that complex algorithms can be implemented by combining algorithms from the STL, so consider STL as the first choice before writing algorithms manually.
STL algorithm concepts
To get a better understanding of the STL algorithms, it's good to know a bit about the concepts and common patterns used by all STL algorithms.
Algorithms operate on iterators
The algorithms in the STL library operate only on iterators, not containers (that is, std::vector
, std::map
, and so on). Basically, an iterator could be considered an object with the same properties as a regular C-pointer; it can be stepped to the next element and dereferenced (if pointing to a valid address). The algorithms only use a few of the operations that a pointer allows, although...