Writing and using generic algorithms
The Algorithm library contains generic algorithms. To keep things as concrete as possible here, I will show an example of how a generic algorithm can be implemented. This will provide you with some insights into how to use the standard algorithms and at the same time demonstrate that implementing a generic algorithm is not that hard. I will intentionally avoid explaining all the details about the example code here, because we will spend a lot of time on generic programming later on in this book.
In the examples that follow, we will transform a simple non-generic algorithm into a full-fledged generic algorithm.
Non-generic algorithms
A generic algorithm is an algorithm that can be used with various ranges of elements, not only one specific type, such as std::vector
. The following algorithm is an example of a non-generic algorithm that only works with std::vector<int>
:
auto contains(const std::vector<int>& arr...