Function Pointers

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?

Abstract art representing computer programming

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(),

  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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved