Function objects in STL (funciont objects)

Source: Internet
Author: User
Tags modulus

Summary
A function object (function object or functor) is anything that can appear in the form of a function call. A common function is obviously a function object, and a function pointer is also. More generally, a class that defines operator () is also.

Description
The basic concepts of function objects include generator, unary function (unary function), binary function (Binary Function): they indicate that they can use F (), F (x ), function objects in the form of f (x, y. When these functions can be extended to ternary funtion or even more, but no STL algorithm actually uses function objects with more than two parameters. All other functional objects defined by STL are refined by these three basic concepts (refinements ).

Returning a function object of the bool type is a very important type of function object. An unary function that returns the bool value is called predicate (predicate), and the binary function that returns the bool value is called binary predicate (Binary predicate ).

There are important but subtle differences between function objects and adaptable function objects. Generally, although a function objects has requirements on the type of its parameters, the Operator () can be overloaded, either a template or both. That is to say, there is no accurate way to obtain the parameter and return type information of this function objects. However, an adaptable function objects must specify its parameters and return value type in the form of typedef. For example, if type f0 is an adaptable function objects model, you must define F0: result_type. Similarly, if F1 is an adaptable unary function objects model, F1: argument_type and F1: result_type must be defined. If F2 is an adaptable binary function objects model, therefore, F2: first_argument_type, F2: second_argument_type, and F2: result_type must be defined. STL provides the basic classes unary_function and binary_function to simplify the model definition of adaptable unary functions and adaptable binary functions.

Adaptable function objects is very important because they can be used by function object adaptors to operate and control other function objects. STL provides many function object adaptors, including unary_negate, unary_compose, and binary_compose, which are used to combine function objects.

Finally, STL includes many predefined function objects, including operators (plus, minus, multiplies, divides, modulus, and negate), arithmetic comparison (equal_to, not_to _to, greater, less, greater_equal and less_equal), and logical operations (logical_and, logical_or, and logical_not ). In this way, you can combine complex operations without manually writing new function objects.

Example
Fill a vector <int> with a random number. Here, a function pointer is a function object.
Vector <int> V (100 );
Generate (V. Begin (), V. End (), RAND );

Sorts a vector by the absolute value <int>. Here, the function object is a user-defined class type.

Struct less_mag: Public binary_function <Double, double, bool> {
Bool operator () (Double X, Double Y) {return FABS (x) <FABS (y );}
};

Vector <double> V;
...
Sort (V. Begin (), V. End (), less_mag ());

Sums a vector <int>. Here, the function object is a user-defined type that can save the state.

Struct adder: Public unary_function <double, void>
{
Adder (): sum (0 ){}
Double sum;
Void operator () (Double X) {sum + = x ;}
};

Vector <double> V;
...
Adder result = for_each (V. Begin (), V. End (), adder (); [3]
Cout <"the sum is" <result. Sum <Endl;

Delete all elements greater than 100 and less than 1000 in list <int>.

List <int> L;
...
List <int>: iterator new_end =
Remove_if (L. Begin (), L. End (),
Compose2 (logical_and <bool> (),
Bind2nd (greater <int> (), 100 ),
Bind2nd (less <int> (), 1000 )));
L. Erase (new_end, L. End ());

Appendix
Concept concepts

Generator
Unary Function
Binary Function

Predicate
Binary Predicate

Adaptable Generator
Adaptable unary Function
Adaptable Binary Function
Adaptable Predicate
Adaptable binary Predicate

Type types
Plus
Minus
Multiplies (formerly called times)
Divides
Modulus,
Negate
Performance_to
Not_0000_to
Greater
Less
Greater_equal
Less_equal,
Logical_and
Logical_or
Logical_not
Subtractive_rng
Identity
Project1st
Project2nd
Select1st
Select2nd
Unary_function
Binary_function
Unary_compose
Binary_compose
Unary_negate
Binary_negate
Binder1st
Binder2nd
Pointer_to_unary_function
Pointer_to_binary_function

Function Functions
Compose1
Compose2
Not1
Not2
Bind1st
Bind2nd
Ptr_fun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.