...

/

Use Lambdas as Predicates with the Algorithm Library

Use Lambdas as Predicates with the Algorithm Library

Learn to use lambdas as predicates with the Algorithm library.

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:

Access this course and 1400+ top-rated courses and projects.