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?

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.

This Question is from the Lesson:

### Function Pointers

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

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

This Question is from the Lesson:

### Function Pointers

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

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.