Use Lambdas as Predicates with the Algorithm Library

Learn to use lambdas as predicates with the Algorithm library.

We'll cover the following

Some functions in the algorithm library require the use of a predicate function. A predicate is a function (or functor or lambda) that tests a condition and returns a Boolean true/false response.

How to do it

For this recipe, we will experiment with the count_if() algorithm using different types of predicates:

  • First, let’s create a function for use as a predicate. A predicate takes a certain number of arguments and returns a bool. A predicate for count_if() takes one argument:

Get hands-on with 1200+ tech skills courses.