STL programming practice seven: try to define the Function Object in the form of Class

zhaozj2021-02-11  201

STL programming practice seven: try to define the Function Object in the form of Class

Yuan Xiakai

South China University of Technology Computer Research Institute North District R & D

Email - ccplusplus@21cn.com

Foreword

When using STL, you will use Function Object, especially when writing a generic algorithm and allowing behavior to parameterize. Function Object is simple to speak two: 1. Function pointer (the simplest Function Object). 2. Class in the form of Function Object.

1. Function pointer (the easiest Function Object)

Function pointer points to a function, the function pointing is generally a full-class function. Behavior can be parametric by a correspondence.

INT A [5] = {1, 2, 3, 4, 5};

Vector Obj (A, A 5);

Find_if (A, A 5, Equalcmp); // Find the first element equal to 5

The equalcmp here is a function pointer that points to a full-class function equalcmp.

Bool Equalcmp (INT X) {RETURN X == 5;}

Here, through the incoming function pointer, we can make the algorithm FIND_IF to find according to our definition behavior.

2. Function Object in the form of Class

Simple point is to define the class of the Operator () member function. Such classes can be called like a function. If equalcmp is such a class, the ECFO is an object, and the following functions can be made:

ECFO (); // Similar to a free-array, of course, there is a parameter.

Take the previous example, define the equal_5 into the FUNTION Object in the form of the class, as follows:

Struct Equalcmp

{

Equalcmp (int x = 5): DATA (X) {}

Bool Operator () (int x) {return data == x;}

Int data;

}

After defining equalcmp, we can use it to make a Find_IF query.

Find_if (A, A 5, Equalcmp (5)); // Find the first element equal to 5

Its behavior is the same, pay attention to EqualCMP (5), which is used to generate a temporary EqualCMP object to invoke the operator () member function.

3. Comparison of both

Functor (another name of Function Object) in the form of function pointer seems to be not flexible, because if equalcmp doesn't look for equal to 5 elements, change 4, it can't be used. You change it, or rewrite one. Haha! However, Class's Functor (more convenient to write is more ^ _ ^) can reduce this boring repetitive labor. Change 4! , Isn't it to find 4, no problem.

Find_if (A, A 5, Equalcmp (4));

Get it! Simple, convenient, easy to use. If you use a function metrics to implement this function, it is more difficult because it is not possible to store local states, and if you want to implement global variables. If you need multiple functors, multiple global variables are needed, don't you try to use global variables? Advice on some experts. But some people will stubbornly constructed the problem of the efficiency of the class of Functor because of the constructors and destructors. They seek a solution that they may find a method:

Template

BOOL Equalcmp (INT X)

{RETURN X == N;} Find_if (A, A 5, Equalcmp <4> (4)); // Find 4, VC (X), G 2.91.57 (x), CB (O)

Can this method? Have you tried it? If you didn't try it! This can be on some compilers, some can't. However, it is absolutely able to call Equalcmp <4> (4); if it has been called so that the Find_IF (A, A 5, Equalcmp <4> (4)) can be used. It may be a problem with the compiler to generate the functional timing of the Function Template entity.

Assume that this can be, but this is not generally. You can use the form of the class to write more general Functor to meet all data types.

Template

Struct Equalcmp

{

Equalcmp (T x = 5): DATA (x) {}

Bool Operator () (T x) {Return Data == x;}

T data;

}

Find_if (A, A 5, Equalcmp (4));

Now Equalcmp can use any data type, this will use the function pointer can't be done! Because you can't write such a function to let it refer. Haha! Pine mouth, drink it again. There is also a function pointer unable to use Adaptable Function Object because it has no nested definition data type. Many function pointers cannot complete the action. Is that the function pointer is not used? No, when you already have a function, you can use a function pointer without rewriting a Class form in the Functor. There is also a function pointer to convert through Function Object Adaptor to Adaptable Function Object. Just say so much!

In short! If you don't have a ready-made function, define the Function Object in the form of the class.

Remnant:

This article is my personal learning summary. If there is any mistake, please refer to it.

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

New Post(0)