<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.
std::function
class template for storing, copying, and invoking any Callable targetplus
, minus
, multiplies
, divides
, modulus
, negate
equal_to
, not_equal_to
, greater
, less
, greater_equal
, less_equal
logical_and
, logical_or
, logical_not
bind
: Creates a new function object based on an existing function objectmem_fn
: Converts a pointer to a member function into a function objectfunction
: A general-purpose polymorphic function wrapperreference_wrapper
: A class template that wraps a reference in a copyable, assignable objecthash
: A function object for computing hash values#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;
}
std::greater<int>()
as a comparison function object for sorting#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;
}
std::function
can store and invoke different types of callable objectsstd::bind
to create a new function object with bound arguments#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;
}
std::function
objectstd::function
can be used with standard algorithms#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;
}
std::bind
to create a new function object from a member function<functional>
are often more efficient than function pointersstd::function
can have a performance overhead compared to direct function calls or functorsstd::bind
less necessary due to lambda expressionsstd::function
, be aware of the potential for null function wrappersThe <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.
[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