C++ Standard Library<functional>
In the C++ standard library<functional>The header file provides a set of function templates that allow us to use function objects as parameters to algorithms, or as return values of algorithms. Function objects are those that overloadoperator()objects that can be called like ordinary functions.
In C++, a function object is a special class that overloadsoperator()to allow objects to be called like functions. This allows us to pass behavior as objects, increasing the flexibility and reusability of code.
Syntax
To use<functional>For the functionality in the header file, you need to include this header file in your C++ program:
#include <functional>
Common function objects
<functional>The header file defines some commonly used function objects, including:
std::function: a general-purpose polymorphic function wrapper.std::bind: used to bind arguments to functions.std::plus、std::minus、std::multiplies、std::divides、std::modulus: basic arithmetic operations.std::equal_to、std::not_equal_to、std::greater、std::less、std::greater_equal、std::less_equal: comparison operations.std::unary_negate、std::binary_negate: logical negation operation.std::logical_and、std::logical_or、std::logical_not: logical operations.
Example
Usagestd::function
std::functionis a template class that can store, call, and copy any callable object, such as functions, lambda expressions, or function objects.
Example
#include <functional>
void greet() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
std::function<void()> f = greet; // Using a function
f(); // Output: Hello, World!
std::function<void()> lambda = []() {
std::cout << "Hello, Lambda!" << std::endl;
};
lambda(); // Output: Hello, Lambda!
return 0;
}
Usagestd::bind
std::bindAllows us to create a callable object that, when invoked, binds the given arguments to a function or function object.
Example
#include <functional>
int add(int a, int b) {
return a + b;
}
int main() {
auto bound_add = std::bind(add, 5, std::placeholders::_1);
std::cout << bound_add(10) << std::endl; // Output: 15
return 0;
}
In this example,std::placeholders::_1is a placeholder that, when callingbound_add, will be replaced by the actual arguments.
using comparison function objects
Comparison function objects can be used in algorithms, such asstd::sort。
Example
#include <vector>
#include <algorithm>
#include <functional>
bool compare(int a, int b) {
return a < b;
}
int main() {
std::vector<int> v = {5, 3, 9, 1, 4};
std::sort(v.begin(), v.end(), compare); // Using a custom comparison function
for (int i : v) {
std::cout << i << " "; // Output: 1 3 4 5 9
}
std::sort(v.begin(), v.end(), std::less<int>()); // Using standard library comparison function objects
for (int i : v) {
std::cout << i << " "; // Output: 1 3 4 5 9
}
return 0;
}
<functional>The header file is a very powerful part of the C++ standard library. It provides a rich set of function objects and utilities, allowing us to write more flexible and reusable code. By using function objects, we can pass behavior as parameters, or store them in containers, thereby achieving more advanced programming techniques.