Often you need to search through an array or list to find a specific element and of course you need this search to be as fast and efficient as possible. One of the best ways to do this is using a binary predicate function.
A binary function is a function object (which are also called Functors ) and is any object which can be called as if it was a function. Depending on your language and platform of choice, Function objects are also known as callback functions , function pointers and delegates (.NET). Generally, there are three types of function objects:
- Generators - function with no arguments
- Unary Functions - function with one argument
- Binary Functions - functions with two arguments
A function object which takes one parameter (i.e. unary function ) and returns a bool are treated as a special case and are called Predicate functions.
How do we use it? Say we have a simple data structure called ContactData to represent a Contact in an Address book as shown in the code snippet below. We also define a predicate function called FindAContact. Now we need to use this predicate function and define another function called findContact. The findContact function in turn uses find_if . find_if takes three parameters, the start of the iterator, the last element and the predicate to use. It returns the first iterator it finds in a given range for which the predicate holds. If no matches are found then the last element in the iterator is returned.
We also need to ensure we have the relevant includes for this to compile and link properly hence include’s below.
The code snippet below shows all that we have discussed.