Using Function Pointers with STL Algorithms

Can I use function pointers with STL algorithms like std::find_if or std::sort? How can I pass a function pointer as a predicate or comparison function?

Yes, you can use function pointers with STL algorithms that accept predicates or comparison functions, such as std::find_if or std::sort. These algorithms are designed to work with callable objects, which include function pointers. Here's an example:

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

bool isEven(int num) { return num % 2 == 0; }

bool descendingOrder(int a, int b) {
  return a > b;
}

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

  auto evenNumber = std::find_if(
    numbers.begin(), numbers.end(), isEven);  

  if (evenNumber != numbers.end()) {
    std::cout << "First even number: "
      << *evenNumber << '\n';
  }

  std::sort(numbers.begin(), numbers.end(),
    descendingOrder);  

  std::cout << "Numbers in descending order: ";
  for (const auto& num : numbers) {
    std::cout << num << ' ';
  }
  std::cout << '\n';
}
First even number: 2
Numbers in descending order: 10 9 8 7 6 5 4 3 2 1

In this example, we have two functions: isEven, which checks if a number is even, and descendingOrder, which compares two numbers for sorting in descending order.

We use std::find_if to find the first even number in the numbers vector. We pass isEven as the predicate function pointer, and it returns an iterator to the first element that satisfies the condition. If no element is found, it returns numbers.end().

We also use std::sort to sort the numbers vector in descending order. We pass descendingOrder as the comparison function pointer, which determines the sorting order.

Function pointers can be used seamlessly with STL algorithms, providing a way to customize the behavior of the algorithms based on specific conditions or comparison criteria.

However, it's worth noting that in modern C++, it's more common to use lambda expressions or function objects (functors) instead of function pointers with STL algorithms. Lambdas and functors offer more flexibility and can capture local state, making the code more expressive and readable.

Nonetheless, function pointers remain a valid and useful tool when working with STL algorithms, especially when you have existing functions that match the required signature or when you need to pass functions as arguments to other functions.

Function Pointers

Learn about function pointers: what they are, how to declare them, and their use in making our code more flexible

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Storing Function Pointers
How can I store multiple function pointers of the same type in a collection like a vector or array?
Pointers to Member Functions
Is it possible to create a pointer to a member function of a class? If so, how is it different from a regular function pointer?
Capturing this in Lambdas
How can I use a lambda to create a function pointer that calls a member function of my class?
Function Pointers and Const
How does const-correctness apply to function pointers? Can I have a pointer to a const function?
Function Pointers and Inheritance
Can I assign a derived class member function to a base class function pointer? How does inheritance affect function pointers?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant