Predicates

In C++, a predicate is a function or function object (functor) that takes one or more arguments and returns a boolean value.

Predicates are commonly used in algorithms and operations that require evaluating conditions or making decisions based on specific criteria.

They play a vital role in STL algorithms as they provide a way to specify custom conditions for sorting, searching, filtering, and other operations.

Example of Predicate:


Let’s demonstrate a simple example of using a predicate to filter even numbers from a vector.

Code:

#include <iostream>
#include <vector>
#include <algorithm>

// Predicate function to check if a number is even
bool isEven(int num) {
    return num % 2 == 0;
}

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // Use the std::remove_if algorithm with the isEven predicate to remove odd numbers
    numbers.erase(std::remove_if(numbers.begin(), numbers.end(), [](int num) { return !isEven(num); }), numbers.end());

    // Print the filtered vector containing only even numbers
    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Output:

2 4 6 8 10

Explanation of the Code:


In this example, we use a predicate function isEven to check if a number is even or not. The predicate takes an integer argument and returns true if the number is even; otherwise, it returns false.

We then use the std::remove_if algorithm along with the isEven predicate to filter even numbers from the numbers vector. The remove_if algorithm rearranges the elements in the vector so that all elements satisfying the condition (i.e., odd numbers) are moved to the end of the vector. It returns an iterator pointing to the first element beyond the valid range, which is then used with the erase method to remove the unwanted elements from the vector.

As a result, the vector numbers contains only even numbers after filtering, which are then printed in the output.

Applications of Predicates:


Predicates are used extensively in various STL algorithms and other situations where you need to evaluate conditions, such as:

  • Sorting with custom criteria.
  • Searching for elements based on specific conditions.
  • Filtering elements based on certain properties.
  • Making decisions in complex algorithms.
  • Grouping and partitioning elements based on specific characteristics.

Advantages of Predicates:

  1. Reusability: Predicates allow you to define custom conditions that can be reused across multiple algorithms and operations.
  2. Customization: Predicates provide a way to tailor algorithms to specific requirements without modifying the algorithm’s code directly.
  3. Clarity: Using predicates makes the code more readable and expressive as the condition logic is encapsulated in a separate function or functor.

Disadvantages of Predicates:

  1. Additional code: Predicates may require extra code to define and manage, which can increase code complexity in some cases.
  2. Function overhead: For simple conditions, the use of a predicate might introduce some function call overhead, but this is usually negligible for most cases.