Function object functor
Operators () are implemented for any type. These operators are called call operators or application operators. STL mainly uses function objects in containers and algorithms of sorting conditions;
Two advantages1) function objects can contain States; 2) function objects are of a type and can be used as template parameters;
Create a function object
You need to create a type and implement the () operator;
class Functor{public: int operator()(int a, int b) { return a < b; }};int main(){ Functor f; int ans = f(1, 3);}
> F () seems to call a function, but actually calls the () operator of the function class. It is similar in syntax;
Function objects and containers
In STL, the <functional> header file contains function objects. One purpose of function objects is to serve as the sort basis of containers. e.g. Set container declaration:
template < class Key, class Traits=less<Key>, class Allocator=allocator<Key> >class set
> The second template parameter of set is the function object less. If the first parameter is smaller than the second parameter, true is returned. Some containers need a method to compare two elements for sorting, in this case, you can use a function object. You can create a function object to define your own sorting method and specify it in the template parameter list;
Function objects and Algorithms
Another purpose is to prepare the algorithm. e.g. remove_if algorithm declaration:
template<class ForwardIterator, class Predicate> ForwardIterator remove_if( ForwardIterator _First, ForwardIterator _Last, Predicate _Pred );
> The last parameter of remove_if is a function object. A boolean value is returned. If the return value is true, the elements are removed from _ first to _ last; you can use any function object defined in <functional> as the parameter _ Pred, or you can create it yourself;
<Refer to> http://msdn.microsoft.com/zh-cn/library/vstudio/aa985932.aspx
--- End ---
Function object
The function object is actually a class that implements the operator () and bracket operators, instead of a function pointer;
NoteUnlike the definition of function pointers, the call method is similar. Function objects can carry additional data, but function pointers do not work;
E.g. function object to save data
Class less {public: less (INT num): n (Num) {} bool operator () (INT value) {return value <n ;}private: int n ;}; // --- less lessobj (10); const int size = 5; int array [size] = {50, 20, 9, 7, 60 }; int * Pi = find_if (array, array + size-1, less (50); // point to 20
> The simplest solution for a function to accept both function pointers and function objects is the template.
Template <typename func> int count_n (int * array, int size, func) {int COUNT = 0; For (INT I = 0; I <size; ++ I) if (func (array [I]) Count ++; return count;} bool less10 (INT value) {return value <10 ;} // --- output 2 cout <count_n (array, size, less (10) <Endl; cout <count_n (array, size, less10) <Endl;
> Another function pointer exclusive to the function object: encapsulate the class member function pointer;
Save the class object pointer to the function object, and you can use the Class Object (Instance) pointer to call the corresponding class Object member function;
Template <typename o> class memfun {public: memfun (void (O: * f) (const char *), O * o): pfunc (F), pobj (o) {} void operator () (const char * Name) {(pobj-> * pfunc) (name);} PRIVATE: void (O: * pfunc) (const char *); O * pobj;}; Class A {public: void doit (const char * Name) {cout <"hello" <name <"! ";}}; // --- A; memfun <A> call (& A: doit, & A); // save :: doit pointer to call ("world ");
> Although the function object can save the member function pointer and call information, it can be called like a function pointer, but the definition of a function object can only implement one member function pointer with a specified number of parameters;
E.g. mem_fun in the standard library is a function object and can only support two member function pointers of 0 and 1 parameter; int A: func () or void :: func (INT); etc.
The boost Library provides the boost: function class. By default, 10 parameters are supported, up to 50;
<Refer to> http://www.cnblogs.com/ly4cn/archive/2007/07/21/826885.html
--- End ---