The function objects in STL, most STL generic algorithms (and some container classes) can take a function object as parameters. The purpose of introducing function objects is to change the function of the algorithm so as to enhance the universality of the algorithm.
A function object refers to a piece of code entity, which can take no parameters or have several parameters, its function is to obtain a value, or change the state of the operation. In C + + programming, any ordinary function satisfies this definition, and any object that overloads a class of operator operator () also satisfies this definition, called a function object .
Common functions
int multfun (intint y) { return x*y;}
Or an object of type multiply as defined below Multfunobj
class multiply{public: intoperator() (intint Const {return x*y;} };
Multiply multfunobj;
Both of the above methods can be called in the following way:
int product1 = Multfun (3, 7);
int product2 = Multfunobj (3, 7);
1. Passing function arguments through function pointers
#include <iostream>#include<cassert>#include<vector>using namespacestd;intMultfun (intXinty) { returnx*y;} Template<typename Inputiterator, TypeName t>T Accumulate1 (inputiterator First, inputiterator last, T init, t (*binary_function) (t x, t y)) { while(First! =Last ) {init= (*binary_function) (INIT, *First ); ++First ; } returnInit;}intMain () {cout<<"demonstrating function pointer passing."<<Endl; intx[5] = {2,3,5,7, One}; Vector<int> Vector1 (&x[0], &x[5]); intProduct = Accumulate1 (Vector1.begin (), Vector1.end (),1, &multfun); ASSERT (Product==2310); cout<<"----OK."<<Endl; return 0;}
Output---OK
A fundamental problem with this traditional approach is that its versatility is not strong enough, for example, a function pointer prototype can be written
T (*binary_function) (t X, t y);
and can match int multifun (int, int), but if it is written as T (*binary_function) (const t& x, const t& y), so that it is more efficient in the case of large T-class objects, The Multifun will not match, and the function must be overridden with the prototype int multifun (const int&, cosnt int&).
Another problem, in the sense of efficiency, is that in accumulate1, you have to pass a pointer to a function that is passed in as a parameter, and you can only call the function in the form of an out-of-line (the first argument is passed to the function, and then the control is handed over to the function. The return value is copied to the calling domain, and the control is finally transferred to the calling domain of the function. In the case where the called function is Mutifun, the computation inside the function requires only one or two machine instructions, and these steps are much more expensive.
2. Defining the superiority of function objects through template parameters
#include <iostream>#include<cassert>#include<vector>using namespacestd;template<typename Inputiterator, TypeName T, TypeName binaryfunction>T Accumulate (inputiterator first, Inputiterator last, T init, binaryfunction binary_function) { while(First! =Last ) {init= Binary_function (init, *First ); ++First ; } returnInit;}classmultiply{ Public: int operator()(intXintYConst{returnx*y;} }; Multiply multfunobj;intMain () {cout<<"demonstrating function pointer passing."<<Endl; intx[5] = {2,3,5,7, One}; Vector<int> Vector1 (&x[0], &x[5]); intProduct = Accumulate (Vector1.begin (), Vector1.end (),1, Multfunobj); ASSERT (Product==2310); cout<<"------OK"<<Endl; return 0;}
With this definition of accumulate, the last actual parameter of the function needs to be met only: with two parameters, the types are T1 and T2, where type T can be converted to T1,inputiterator value type can be converted to T2,
And the return value type of the parameter can be converted to T, so that the definition can accept more kinds of function prototypes than the overly strict type requirements passed by the function pointer.
In terms of performance, because a function object that can be passed as a parameter is defined by a template parameter and an overloaded operator (), the compiler can call the Binary_function function inline (inline) in the accumulate function body.
This completely eliminates the additional overhead of pointer parsing and external calls.
In addition, some additional information can be defined in the class definition, so that objects of the class carry some additional information.
Next we'll take a simple example of some basic uses of class objects.
Refer to the Standard Template Library self-study tutorial and reference manual--STL C + + programming
Reference: http://blog.csdn.net/bonchoix/article/details/8050627
function objects in C + + (i)