Funciont Objects in STL

xiaoxiao2021-03-06  70

SUMMARY A function object (Function Object or functor) is simple to say that anything capable of appearing in the form of a function call. A normal function is obviously a function object, and the function pointer is also, more general, a Class that defines how Operator () is also.

Describe the concept of basic function objects with generator, unary function, binary function: They each represent function that can occur in F (), f (x), f (x, y) Object. When these can be extended to Ternary Funtion, but in fact no STL algorithm has used a function object of more than two parameters. All other STL definitions of function objects are refinered in these three basic concepts (Refinements).

Returns a Bool type Function object is a considerable important type of function object. A Unary Function returned to the BOOL value is called a Predicate, and the Binary Function that returns the BOOL value is called a binary futicate.

There is important but some subtle differences between Function Objects and Adaptable Function Objects but some subtle differences. Generally, although a Function Objects requires the type of parameters, operators operator () can be overloaded, or both, or both. That is, the parameters of this Function Objects are not accurately obtained and the information of the return type. However, an Adaptable Function Objects must specify his parameters and returns a value type in the form of TypeDef. For example, the type F0 is the model of Adaptable Function Objects, then you must define F0 :: Result_Type. Similarly, if F1 is Adaptable Unary Function Objects model, then F1 :: argument_type and F1 :: result_type must be defined; if F2 is Adaptable Binary Function Objects model, you must define the F2 :: first_argument_type, F2 :: second_argument_type and F2 :: result_type. STL provides the definition of the base class unary_function and binary_function to simplify the model of Adaptable Unary Functions and Adaptable Binary functions.

Adaptable Function Objects is very important because they can be used by the Function Object Adaptors (Function Object Adapter, Used to Control Other Functions). STL provides a number of Function Object Adaptors, including unary_negate, unary_compose, and binary_compose, which are used to combine the function objects.

Finally, STL includes many different predefined function objects, including operators (Plus, Minus, Multiplies, Divides, modulus, and negate), arithmetic comparisons (Equal_to, not_equal_to, greater, less, greater_equal, and less_equal), and logical operations (Logical_and, Logical_or and Logical_not). This way you can use these function objects without manually write a new function object, and use these function objects to combine fairly complex operations.

An example will populate a vendor number with a random number. Here, a function pointer is a function object. Vector v (100); generate (v.begin (), v.end (), rand);

Sort a vector according to the absolute value size. Here, the function object is a user-defined Class type. Struct Less_mag: public binary_function {bool operator () (double x, double y) {returnif (x)

Vector v; ... sort (v.begin (), v.end (), less_mag ());

Squirate a vector . Here, the function object is a user-defined type that can be saved.

Struct adder: public unary_function {adder (): sum (0) {} Double SUM; Void Operator () (Double X) {Sum = x;}}

Vector v; ... adder result = for_each (v.begin (), v.end (), adder ()); [3] cout << "the suuse" << result.sum << endl ;

Delete Elements greater than 100 and less than 1000 in List

List L; ... list :: item new_end = remove_if (L.BEGIN (), L.End (), compose2 (logical_and (), bind2nd (Greater (), 100), BIND2ND (LESS (), 1000))); L.RASE (new_end, l.end ());

Schedule concept ConceptsGenerator unary function binary function

Predicate binary predicate

Adaptable Generator Adaptable Unary Function Adaptable Binary Function Adaptable Predicate Adaptable Binary PREDICATE ADAPTABLE PREDICATE

Type Typesplus minus multiplies (formerly called times) divides modulus, negate equal_to not_equal_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 FunctionsCompose1Compose2Not1Not2 bind1stbind2ndptr_fun

转载请注明原文地址:https://www.9cbs.com/read-91994.html

New Post(0)