4, STL function object (understand)

4 STL function object

4.1 function object

4.1.1 function object concept

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, which is also called an imitation function

Essence:

A function object is a class, not a function

4.1.2 function object usage

characteristic:

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

Example:

#include <string>

//1. When a function object is used, it can be called like an ordinary function, with parameters and return values
class MyAdd
{
public :
	int operator()(int v1,int v2)
	{
		return v1 + v2;
	}
};

void test01()
{
	MyAdd myAdd;
	cout << myAdd(10, 10) << endl;
}

//2. Function objects can have their own state
class MyPrint
{
public:
	MyPrint()
	{
		count = 0;
	}
	void operator()(string test)
	{
		cout << test << endl;
		count++; //Count usage times
	}

	int count; //Internal self state
};
void test02()
{
	MyPrint myPrint;
	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
void doPrint(MyPrint &mp , string test)
{
	mp(test);
}

void test03()
{
	MyPrint myPrint;
	doPrint(myPrint, "Hello C++");
}

int main() {

	//test01();
	//test02();
	test03();

	system("pause");

	return 0;
}

Summary:

  • The imitation function is very flexible and can be passed as a parameter.

4.2 predicate

4.2.1 predicate concept

Concept:

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

4.2.2 unary predicate

Example:

#include <vector>
#include <algorithm>

//1. Unary predicate
struct GreaterFive{
	bool operator()(int val) {
		return val > 5;
	}
};

void test01() {

	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "Can't find!" << endl;
	}
	else {
		cout << "find:" << *it << endl;
	}

}

int main() {

	test01();

	system("pause");

	return 0;
}

Summary: predicates with only one parameter are called unary predicates

4.2.3 binary predicate

Example:

#include <vector>
#include <algorithm>
//two-place predicate
class MyCompare
{
public:
	bool operator()(int num1, int num2)
	{
		return num1 > num2;
	}
};

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

	//Default from small to large
	sort(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	cout << "----------------------------" << endl;

	//Use the function object to change the algorithm strategy and sort from large to small
	sort(v.begin(), v.end(), MyCompare());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

Summary: predicates with only two parameters are called binary predicates

4.3 built in function object

4.3.1 meaning of built-in function object

Concept:

  • STL has built-in function objects

Classification:

  • Arithmetic imitation function

  • Relational affine function

  • Logical imitation function

Usage:

  • The usage of the objects generated by these imitation functions is exactly the same as that of general functions
  • To use the built-in function object, you need to import the header file #include < functional >

4.3.2 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 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

Example:

#include <functional>
//negate
void test01()
{
	negate<int> n;
	cout << n(50) << endl;
}

//plus
void test02()
{
	plus<int> p;
	cout << p(10, 20) << endl;
}

int main() {

	test01();
	test02();

	system("pause");

	return 0;
}

Summary: when using built-in function objects, the header file #include < functional > needs to be introduced

4.3.3 relation imitation function

Function Description:

  • Implement relationship comparison

Imitation 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

Example:

#include <functional>
#include <vector>
#include <algorithm>

class MyCompare
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};
void test01()
{
	vector<int> v;

	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(40);
	v.push_back(20);

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	//Realize the imitation function by yourself
	//sort(v.begin(), v.end(), MyCompare());
	//STL built-in functor is larger than functor
	sort(v.begin(), v.end(), greater<int>());

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

Summary: greater < > greater than is the most commonly used in relational imitation functions

4.3.4 logic imitation function

Function Description:

  • Realize logical operation

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_ Not < T > / / not logical

Example:

#include <vector>
#include <functional>
#include <algorithm>
void test01()
{
	vector<bool> v;
	v.push_back(true);
	v.push_back(false);
	v.push_back(true);
	v.push_back(false);

	for (vector<bool>::iterator it = v.begin();it!= v.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//Logical non carries the v container into v2 and performs logical non operations
	vector<bool> v2;
	v2.resize(v.size());
	transform(v.begin(), v.end(),  v2.begin(), logical_not<bool>());
	for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

Conclusion: there are few practical applications of logic imitation function, so you can understand it

Keywords: C++ Back-end

Added by CodeMama on Tue, 01 Mar 2022 07:30:32 +0200