Writing and Using Generic Algorithms

Learn how to write and use generic algorithms with various data structures.

The Algorithm library contains generic algorithms. To keep things as concrete as possible here, we will show an example of how a generic algorithm can be implemented. This will provide us 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. We will intentionally avoid explaining all the details about the example code here because we will spend a lot of time on generic programming later in this course.

We will transform a simple non-generic algorithm into a full-fledged generic one in the following examples.

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>:

Get hands-on with 1200+ tech skills courses.