"Function object" in C + +

Function object

Concept of function object

  • Concept:
    ○ classes that overload function call operators, and their objects are often called function objects;
    ○ when the function object uses overloaded (), its behavior is similar to function call, so it is usually called imitation function;
  • Essence:
    ○ function object (imitation function) is a class, not a function;

Function object call

  • characteristic:
    ○ when a function object is used, it can be called like an ordinary function, with parameters and return values;
    ○ function object is beyond the concept of ordinary function, and function object can have its own state;
    ○ function objects can be passed as parameters;
#include <iostream>
#include <string>
using namespace std;

class MyAdd {
public:
	int operator()(int a, int b) {
		return a + b;
	}
};

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

//2. Function object is beyond the concept of ordinary function, and function object can have its own state;
class MyPrint {
	int count = 0;
public:
	MyPrint() {
		this->count = 0;
	}
	void operator()(string str) {
		cout << str << endl;
		this->count++;
	}

	int getCount() { return this->count; }
};

void test02() {
	MyPrint myPrint;
	myPrint("Give me power!!!");
	myPrint("Give me power!!!");
	myPrint("Give me power!!!");
	cout << "The number of calls is:" << myPrint.getCount() << endl;
}

//Function objects can be passed as arguments
void testPrint(MyPrint& myPrint, string test) {
	myPrint(test);
}

void test03() {
	MyPrint m;
	testPrint(m, "it's time for bed!!!");
}

int main() {
	test01();
	cout << endl;

	test02();
	cout << endl;

	test03();
	return 0;
}
/*
2

Give me power!!!
Give me power!!!
Give me power!!!
Number of calls: 3

it's time for bed!!!

*/

predicate

Concept of predicate

  • Functions that return bool types are called predicates;
  • If operator() accepts a parameter, it is called a unary predicate;
  • If the operator accepts two arguments, it is called a binary predicate;

one-element predicate

  • find_if() – returns the location of the container iterator
    ○ find the corresponding element and return the position of the element iterator;
    ○ if not found, return to the container end();

two-place predicate

  • Use the sort() function
    ○ when the custom method is a function, we only need to write its function name into the sort() algorithm;
    ○ when using function objects, you need to use the method of creating anonymous objects, that is, you need to add () after the class name;

Built in function object

Built in function object meaning

  • Concept: STL has built-in function objects;
  • Classification:
    ○ arithmetic imitation function;
    ○ relational imitation function;
    ○ logic imitation function;
  • Usage:
    ○ the objects generated by these imitation functions have the same usage as general functions;
    ○ when using built-in function objects, the header file #include needs to be introduced;

Arithmetic imitation function

  • Function Description:
    ○ realize four operations;
    ○ among them, negate is a unary operation, and others are binary operations;
  • Imitation function prototype:
    ○ template < class T > t plus < T > / / addition
    ○ template < class T > t minus < T > / / subtraction
    ○ template < class T > t multiples < T > / / multiplication
    ○ template < class T > t divisions < T > / / Division
    ○ template < class T > t module < T > / / take the module
    ○ template < class T > t negate < T > / / negative
#include <iostream>
#include <functional>
using namespace std;

//addition
void test01() {
	cout << "Addition:";
	plus<int> p;
	cout << "a + b = " << p(10086, 10010) << endl;
}
//subtraction
void test02() {
	minus<int> m;
	cout << "Subtraction:";
	cout << "a - b = " << m(10086, 10010);
}



int main() {
	cout << "a = 10086\tb = 10010" << endl;
	test01();
	test02();
	return 0;
}
/*
a = 10086       b = 10010
 Addition: a + b = 20096
 Subtraction: a - b = 76
*/

Relational affine function

  • template<class T> bool equal_ To < T > / / equal to
  • template bool not_equal_to ` / / 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

Logical imitation function

● template<class T> bool logical_and<T>
● template<class T> bool logical_or<T>
● template<class T> bool logical_not<T>

Keywords: C++

Added by huckfinne on Tue, 15 Feb 2022 11:45:57 +0200