Use of function objects in C ++

xiaoxiao2021-03-06  99

Function object

Algorithms with C standard template libraries can reduce many burdes for us, but most of these algorithms need functions or function objects as parameters, such as Sort algorithms used to sort, and its interface is defined as follows:

Template

Void Sort (RandomaccessItemrator First,

RandomaccessITerator Last;

Template

Void Sort (RandomaccessItemrator First,

RandomaccessItemrator Last, Compare Comp);

The first syntax is only suitable for sorting objects belonging to the basic data type. It uses

// General function

Void Print (INT X)

{

IF (x> = 0 && x <= 100) cout << x << "

}

// Function object pointer

TypeDef void (* ptprint) (INT X);

PTPrint PRT = Print;

// An overloaded () operator class

Class isok

{

PUBLIC:

Bool Operator () (int Val)

{

IF (Val <0 || Val> 100)

{

Return False;

}

Return True;

}

}

With the above definition, we can use the above function objects in the form of a function call, such as: for_each (vectora.begin (), vectora.end (), print) printing greater than or equal to 0 less than equal to 100, Transform (Vectora.begin (), VectorB.Begin (), isok ()) Judging whether the range of the vector VECTORA is between 0 100, if the value corresponding to the vector VECTORB is true Otherwise, in the fake, where the data type in the VECTORB is BOOL. In practice, the function object is often divided into three forms, no group function objects, a dollar function object, and binary function object, and ISOK () is a no-reflection object. Non-ginseng function objects and one dollar, binary function objects have great differences, one yuan, binary function object definition needs to inherit the following function prototype:

/ / One dollar function object function prototype

Template

Struct unary_function {

TypeDef arg argument_type;

TypeDef result rings_type;

}

// binary function object function prototype

Template

Struct binary_function {

TYPEDEF arg1 first_argument_type; typedef arg2 second_argument_type;

TypeDef result rings_type;

}

Result returns a value, if we need to define a dollar or binary function object, you need to inherit the prototype of these two function objects, such as:

Class gradeCompare: binary_function {

PUBLIC:

Bool Operator () (Grade X, Grade Y) Const

{

Return x.score> y.score;

}

}

The above is a class of binary function objects. The instance of this class is a binary function object, considering the following situation:

There are many students' grades existing in a vector vector in the form of objects. Now we need to sort these students, using the standard template library Sort algorithm, student grades include many fields, such as students, name, and grades. The object of the field, the object is defined as follows:

Class grade

{

PUBLIC:

GRADE (int ID, string name, int score)

{

ID = ID;

Name = name;

SCIE = score;

}

Int ID;

String name;

int Score;

}

According to the prototype of the Sort Algorithm, we must define a function object for comparative student grades as follows:

Class gradeCompare: binary_function {

PUBLIC:

Bool Operator () (Grade X, Grade Y) Const

{

Return x.score> y.score;

}

}

When defining a function object for sorting, you can directly call the SORT algorithm for sorting.

Sort (Finalgrade.begin (), Finalgrade.end (), gradeCompare ()); // finalgrade is used to store students' performance

The entire source code is as follows:

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#include

#include

#include

Using namespace std; // Using the standard template library namespace

/ / -------------------------------------------------------------------------------------------- ---------------------------

#pragma argsused

Class grade

{

PUBLIC:

GRADE (int ID, string name, int score)

{

ID = ID;

Name = name;

SCIE = score;

}

Int ID;

String name;

int score;

}

// Print student grades

Void Printscore (grade grade)

{

COUT << grade.id << "" << grade.name << "<< grade.score << Endl;

}

// Define function objects used to sort

Class gradeCompare: binary_function {

Public: BOOL Operator () (Grade X, Grade Y) Const

{

Return x.score> y.score;

}

}

Int main (int Argc, char * argv [])

{

Vector finarygrade;

Finalgrade.push_back (grade (1, "a", 56));

Finalgrade.push_back (grade (2, "b", 57));

Finalgrade.push_back (grade (3, "c", 58);

Sort (Finalgrade.Begin (), Finalgrade.end (), gradecompare ());

FOR_EACH (Finalgrade.Begin (), Finalgrade.end (), Printscore;

Return 0;

}

The results are as follows:

3 C 58

2 B 57

1 a 56

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

New Post(0)