## Imitation function (function object)

Imitation function is the early name of C + +, and the finalization of C + + Standard Specification adopts function object

Function object: an object with function characteristics. From the perspective of behavior, the use of imitation function in Chinese is more prominent

From the perspective of calling, it can be called like a function

From the perspective of the callee, the function call operator() defined by the object plays the essential role of the function

Operation function: the only way to take an operation as a parameter of the algorithm is to design the operation (which may have more than several instructions) as a function

There are two ways to design a function:

- Design as a function, and then take a function pointer as a parameter of the algorithm
- The operation is designed as a so-called imitation function (a class at the language level), and an object is generated by the imitation function and used as a parameter of the method

From the perspective of implementation: the imitation function is actually an object that behaves like a function. To realize this function, the function call operator (operator()) must be customized (or rewritten or overloaded) in the class definition. In use, you can add a pair of parentheses after the object to call the operator() defined by the imitation function

### Classification of affine functions

- According to the number of operand s, it can be divided into univariate affine function and binary affine function
- According to the functional division, it can be divided into arithmetic operation arithmetic, relational operation rational and logical operation logical

You need to declare the header file to use the functor

### The key of matching affine functions

Among the six components of STL, the affine function has the smallest volume, the simplest concept and the easiest implementation. It plays a strategic role in STL (the algorithm has different mutation behavior due to the intervention of different affine functions, and the essence of the algorithm is unchanged)

STL imitation functions should be able to be displayed by the function adapter and connected in series like building blocks

To have the ability of matching, each functor must define its own corresponding type, and the root iterator similarly defines its own five corresponding types

The purpose of type is to enable the adapter to fetch and obtain some information of the imitation function. The corresponding types are only typedef s. All necessary operations are completed in the compilation time, which has no impact on the execution efficiency of the program and does not bring any additional burden

The corresponding type of the inverse function is mainly used to represent the function parameter type and return value type. In STL, only univariate functor and binary functor (STL does not support ternary functor) are supported to define the type class respectively. There is no data member or member function in the class, but only some type definitions: the functor only needs to choose to inherit one of the class variables and has the ability of corresponding type

#### unary_function unary functor corresponding type

template <class Arg,class Result> struct unary_function{ typedef Arg argument_type; // Parameter type typedef Result result_type; // return type };

#### binary_function corresponding type of binary functor

template <class Arg1,class Arg2,class Result> struct binary_function{ typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; };

### Divide a variety of imitation functions by function

#### Arithmetic like function

STL built-in arithmetic imitation function supports addition, subtraction, multiplication and modulus negation

//addition plus<T> //subtraction minus<T> //multiplication multiplies<T> //division divides<T> //Mould taking modulus<T> //negative negate<T>

Example:

template <class> struct negate : public unary_function<T,T>{ T operator()(const T& x) const { return -x; } }

#### Relational operation class imitation function

STL built-in relational operator functions support equal to, not equal to, greater than, greater than or equal to, less than or equal to, and less than or equal to, each of which is a binary operator

//be equal to equal_to<T> //Not equal to not_equal_to<T> //greater than greater<T> //Greater than or equal to greater_equal<T> //less than less<T> //Less than or equal to less_equal<T>

#### Logic operation imitation function

STL built-in supports logic operation class imitation function, and supports AndOrNot three operations in logic operation

logical_and<T> logical_or<T> logical_not<T>

#### Verify identity, select and project

Some visiting functions deliberately select or ignore the returned parameters. Use these three. No longer used in design. It's drawing a layer. Ensure indirectness, which is an important tool of abstraction.

##### Identity certificate

Any value will not change through this function.

STL is used in Rb tree set because the key value is a real value

struct identity:public unary_function<T,T>{ const T& operator()(const T&x) const { return x; } };

##### Select select

In an element. Select components, such as the selection of two elements of pair

The first element of pair used in Rb tree in STL is the key value

template <class Pair> struct select1st : public unary_funiction<Pair,typename Pair::first_type>{ const typename Pair::first_type& operator()(const Pair &x)const { return x.first; } };

##### Projection function project

Project one of the two parameters.

template <class Arg1,class Arg2> struct project1st : public binary_function<Arg1,Arg2,Arg1>{ Arg1 operator()(const Arg1&x ,Arg2&) const{ return x; } }