functional


Header: <functional>

The <functional> header is part of the C++ Standard Library and provides a collection of function objects, adaptors, and utilities for working with callable entities. It's particularly useful for creating and manipulating functions and function-like objects in a flexible and generic way.

Key Characteristics

Key Components

Function Objects

  1. Arithmetic operations: plus, minus, multiplies, divides, modulus, negate
  2. Comparisons: equal_to, not_equal_to, greater, less, greater_equal, less_equal
  3. Logical operations: logical_and, logical_or, logical_not

Function Adapters

  1. bind: Creates a new function object based on an existing function object
  2. mem_fn: Converts a pointer to a member function into a function object

Utilities

  1. function: A general-purpose polymorphic function wrapper
  2. reference_wrapper: A class template that wraps a reference in a copyable, assignable object
  3. hash: A function object for computing hash values

Example 1: Using Standard Function Objects

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

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

    std::sort(numbers.begin(), numbers.end(), std::greater<int>());

    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation

Example 2: Using std::function

#include <iostream>
#include <functional>

void print_num(int i) {
    std::cout << i << std::endl;
}

int main() {
    std::function<void(int)> f_display = print_num;
    f_display(42);

    std::function<void()> f_display_31337 = std::bind(print_num, 31337);
    f_display_31337();

    return 0;
}

Explanation

Example 3: Using Lambda Expressions with std::function

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

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

    std::function<bool(int)> is_even = [](int n) { return n % 2 == 0; };

    auto count = std::count_if(numbers.begin(), numbers.end(), is_even);

    std::cout << "Number of even integers: " << count << std::endl;

    return 0;
}

Explanation

Example 4: Using std::bind

#include <iostream>
#include <functional>

class Multiplier {
public:
    int multiply(int a, int b) {
        return a * b;
    }
};

int main() {
    Multiplier m;
    auto times_two = std::bind(&Multiplier::multiply, &m, std::placeholders::_1, 2);

    std::cout << "3 * 2 = " << times_two(3) << std::endl;

    return 0;
}

Explanation

Additional Considerations

Summary

The <functional> header in C++ provides a rich set of tools for working with functions and function-like objects. It offers standard function objects for common operations, adapters for modifying and combining functions, and utilities like std::function for type-erased function wrappers. These components are particularly useful in generic and functional-style programming, allowing for more flexible and expressive code when working with algorithms and callbacks. By leveraging the features of <functional>, C++ developers can write more modular and reusable code, especially when dealing with higher-order functions and complex function compositions.

Citations:

[1] https://en.cppreference.com/w/cpp/header/functional [2] https://www.tutorialspoint.com/cpp_standard_library/functional.htm [3] https://learn.microsoft.com/en-us/cpp/cpp/header-files-cpp?view=msvc-170 [4] https://stackoverflow.com/questions/4686507/lambda-expression-vs-functor-in-c [5] https://learn.microsoft.com/en-us/cpp/standard-library/functional?view=msvc-170 [6] https://stackoverflow.com/questions/11527528/confused-about-the-actual-purpose-of-header-files-in-c [7] https://www.geeksforgeeks.org/when-to-prefer-lambda-expressions-over-functors-in-cpp/

Previous Page | Course Schedule | Course Content