C++STL function object, predicate, built-in function object

Posted by subcool on Fri, 04 Feb 2022 04:21:56 +0100

Function object

Concept:

  • A class that overloads the function call operator. Its object is often called a function object
  • When a function object uses overloaded (), its behavior is similar to that of a function call, so it is also called an imitation function

Essence:

  • An object is a function, not a function

Function object usage

characteristic

  • When a function object is used, it can be called like an ordinary function, with parameters and return values
  • Function object goes beyond the concept of ordinary function. It can have its own state
  • Function objects can be passed as arguments

Examples

class MyAdd 
{
public:
	int operator()(int v1, int v2) {
		return v1 + v2;
	}
};

class MyPrint
{
public:
	MyPrint() {
		count = 0;
	}

	void operator()(string test) {
		cout << test << endl;
		count++;	//Count usage times
	};

	int count;		//Internal state of function
};

void doPrint(MyPrint& mp, string test) {
	mp(test);
}

//1. When a function object is used, it can be called like an ordinary function, with parameters and return values
MyAdd myAdd;
cout << myAdd(10, 10) << endl;

//2. Function object goes beyond the concept of ordinary function. It can have its own state
MyPrint myPrint;
myPrint("hello world");
myPrint("hello world");
myPrint("hello world");
myPrint("hello world");
myPrint("hello world");
cout << "myPrint The number of calls is:" << myPrint.count << endl;
	
//3. Function objects can be passed as arguments
doPrint(myPrint, "hello C++");

Operation results:

predicate

concept

  • Functions that return bool types are called predicates
  • If operator() receives a parameter, it is called a unary predicate
  • If operator() receives a parameter, it is called a binary predicate

Built in function object

STL has built-in function objects

Classification:

  • Arithmetic imitation function
  • Relational affine function
  • Logical imitation function (and, or, non operation)

Usage:

  • The objects and usages generated by these imitation functions are exactly the same as those of general functions
  • When using built-in function objects, the header file #include < functional > needs to be introduced

Arithmetic imitation function

Function description

  • Realize four operations
  • Among them, negate is a unary operation, and others are binary operations

Imitative function prototype

  • Template < class T > t plus < T > / / addition functor
  • Template < class T > t minus < T > / / subtraction function
  • Template < class T > t multiples < T > / / multiplication functor
  • Template < class T > t divisions < T > / / division imitation function
  • Template < class T > t module < T > / / take the imitation function
  • Template < class T > t negate < T > / / take the inverse function

Examples

//negate is a unary imitative function 	 Inverse operation
negate<int>n1;
	cout << n1(50) << endl;		//Output - 50

//plus bivariate affine function 	 Addition operation
plus<int>n2;
cout << n2(10, 20) << endl;		//Output 30

Relational affine function

Function description

  • Implement relationship comparison

Imitative function prototype

  • template<class T>bool equal_ To < T > / / equal to
  • template<class T>bool not_ equal_ To < T > / / not equal to
  • Template < class T > bool greater < T > / / greater than
  • template<class T>bool greater_ Equal < T > / / greater than or equal to
  • Template < class T > bool less < T > / / less than
  • template<class T>bool less_ Equal < T > / / less than or equal to

Examples

vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(50);

//greater 	 Greater than, the descending order is realized, and the result is 50 40 30 20 10
sort(v.begin(), v.end(),greater<int>());

Logical imitation function

Function description

  • Realize logical operation

Imitative function prototype

  • template<class T>bool logical_ And < T > / / logic and
  • template<class T>bool logical_ Or < T > / / logical or
  • template<class T>bool logical_< T> / / logical non

Examples

vector<bool>v2;
v2.push_back(true);
v2.push_back(false);
v2.push_back(true);
v2.push_back(false);

//logical 	 Logical non
//Here, the data of container v2 is transported to container v3, and the reverse operation is performed. The result is 0 1 0 1
vector<bool>v3;
v3.resize(v2.size());
transform(v2.begin(), v2.end(), v3.begin(), logical_not<bool>());

Topics: C++