Basic Introduction to function objects
A function object is a common class object. It is not common because it shows the characteristics of a function, that is, it can call a function like a common function. The call operator is overloaded.
Function objects are generally more flexible than common functions. For example, let's look at the example below.
[Cpp]
Bool GT6 (int data ){
Return data> 6;
}
The purpose of this function is to compare whether a number is greater than 6. To add a user requirement, we need to test whether the numbers entered by a group of 10 users are greater than 1 to 6 ~ 10. If a common function is used, we have to implement 10 such function versions (the function template is not tested for the time being ). In another way, if we use function objects, the results will be very different:
[Cpp]
Class GT_int {
Public:
GT_int (int pn): n (pn ){
}
Bool operator () (int data ){
Return data> n;
}
Private:
Int n;
}
The above is the implementation method of a function object. It reloads the call operator so that it can perform the same operations as the function, and uses the constructor to determine the value to be compared. The following is how it is used:
[Cpp]
Int data
For (int I = 1; cin> data & I <= 10; ++ I ){
GT_int gt (I );
Cout <gt (data) <endl;
}
Function objects in STL
Function objects are very useful in many cases. For example, they can display the basic logic used by algorithms in the specified STL algorithm library. For example, see the following sorting example:
[Cpp]
Sort (svec. begin (), svec. end (), greater <string> ());
Generally, the default sort function uses the '<' operator to sort the elements of the container. In the previous overloaded version, the third parameter greater <string> () is used (), it is a function object defined in the function header file and can compare whether the first operand is greater than the second operand. That is to say, passing this function object will sort the sort function in descending order.
In STL, function objects can be classified as follows based on different parameters and return values:
1. The generator has no parameters.
2. One Parameter of the mona1 Function
3. Two Parameters of binary Functions
4. A single-element predicate a parameter. The return type is bool.
5. Two Binary predicates. The return type is bool.
Let's take a look at the accumulate statement of the function:
[Cpp]
Template <class InputIterator, class T, class BinaryOperation>
T accumulate (InputIterator first, InputIterator last, T init,
BinaryOperation binary_op );
The last parameter must be a binary function.
Function Adapter
The container adapter and iterator adapter have been introduced earlier. Let's take a look at the function adapter today. Like all adapters, function adapters are used to show features of another type of function objects. In many cases, the number of parameters or return value types of our function objects or common functions are not what we want. At this time, we need a function adapter to adapt our function. Function adapters can be divided into two main types:
1. Bind the adapter
This type of adapter is used to adapt a binary function to a mona1 function.
Binder1st binds the adaptation parameter to the first parameter. binder2nd binds the adaptation parameter to the second parameter. As for bind1st and bind2nd, they are actually adapted template functions to avoid lengthy declarations.
Let's take a look at an example:
[Cpp]
Count_if (vec. begin (), vec. end (), bind2nd (less_equal <int> (), 10 ));
Less_equal is a function object provided by STL. It has two parameters, which are used to compare whether the first parameter value is <= the second parameter value. However, count_if requires that the third parameter must be a one-dimensional predicate. Therefore, we use bind2nd to adapt the function object and bind 10 to the second parameter of the function object. Now the cont_if function is actually used to find the number of elements with the value of <= 10 in a given sequence.
2. inverter Adapter
The main function of this type of adapter is to adapt the return value of the function. It reverse the bool value and adapt true to false and false to true.
For example, if we want to calculate the number of elements with an element value greater than 10 in a given sequence, we can write as follows:
[Cpp]
Count_if (vec. begin (), vec. end (), not1 (bind2nd (less_equal <int> (), 10); www.2cto.com
STL provides two inverter adapters, not1 and not2. The parameters of the adaption functions are one and two.
Author: justaipanda