C + + classic problems_ 25 STL function object

I Function object concept

Concept:

  1. The object of a class that overloads the function call operator is often called a function object
  2. 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:

A function object is a class, not a function

characteristic:

  1. When a function object is used, it can be called like an ordinary function, with parameters and return values
  2. Function object is beyond the concept of ordinary function. Function object can have its own state (class attribute)
  3. Function objects can be passed as arguments
/*----------------------------------------------------------------
* Projects: classic question
* Author: Fioman
* Email: geym@hengdingzhineng.com
* Time: March 22, 2022
* Maxim: talk is heap, show me the code^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
// 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 a, int b)
	{
		return a + b;
	}
};

void test_01(void)
{
	// Create a function object
	MyAdd add;
	int a = 10;
	int b = 20;
	// add(a,b) is similar to a function call, but it is not a function call in essence. It is an operator overload, that is, an imitation function
	cout << a << " + " << b << " = " << add(a, b) << endl;
}

// A functor can have its own state (class attribute)
class MyPrint
{
public:
	MyPrint()
	{
		mCount = 0;
	}
	void operator()(string text)
	{
		cout << text << endl;
		mCount++;
	}
	int mCount; // Number of function calls
};

void test_02(void)
{
	MyPrint print;
	print("Fioman");
	print("Fioman");
	print("Fioman");
	print("Fioman");
	cout << "Number of calls: " << print.mCount << endl;
}

void do_print(MyPrint mp, string text)
{
	mp(text);
}

// Can be used as an argument to a function
void test_03(void)
{
	MyPrint print;
	do_print(print, "Hello World!");
}


int main()
{

	test_01();
	test_02();
	test_03();
	system("pause");
	return 0;
}

II one-element predicate

Concept of predicate:

  1. Functions that return bool types are called predicates
  2. If operator() receives a parameter, it is called a unary predicate
  3. If operator() receives two parameters, it is called a binary predicate
/*----------------------------------------------------------------
* Projects: classic question
* Author: Fioman
* Email: geym@hengdingzhineng.com
* Time: March 22, 2022
* Maxim: talk is heap, show me the code^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>

class GreaterFive
{
public:
	// Create a unary predicate. The return value must be of bool type, and there is only one parameter
	bool operator()(int a)
	{
		return a > 5;
	}
};

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

	// GreaterFive() is an anonymous variable
	// It is also a unary predicate find_ The return value of if is an iterator
	// If found, return the position of this element. If not found, return end();
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it != v.end())
	{
		cout << "eureka,Found element: " << *it << endl;
	}
	else
	{
		cout << "No element greater than 5 was found" << endl;
	}
}

int main()
{
	test_01();

	system("pause");
	return 0;
}

result:

III two-place predicate

Predicates whose formal parameters have two parameters are called binary predicates

/*----------------------------------------------------------------
* Projects: classic question
* Author: Fioman
* Email: geym@hengdingzhineng.com
* Time: March 22, 2022
* Maxim: talk is heap, show me the code^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>

// two-place predicate
class MyCompare
{
public:
	bool operator()(int a,int b)
	{
		return a > b;
	}
};

void test_01(void)
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(rand()% 10);
	}
	// The default sorting algorithm is to sort from small to large
	sort(v.begin(), v.end());
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << " ";
	}
	cout << endl;

	// Change the sorting rule from big to small
	cout << "------------------------------" << endl;
	sort(v.begin(), v.end(), MyCompare());

	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << " ";
	}
	cout << endl;
}
int main()
{
	test_01();
	system("pause");
	return 0;
}

IV Built in function object

① Concept

STL has built-in function objects

② Classification

  1. Arithmetic imitation function (function object)
  2. Relational imitation function (function object)
  3. Logical imitation function (function object)

③ Usage

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

④ Arithmetic imitation function

Function:

  1. Realize four operations
  2. Among them, negate is a unary operation, and others are binary operations

Imitative function prototype

  1. Template < class T > t plus < T > / / addition functor
  2. Template < class T > t minus < T > / / subtraction function
  3. Template < class T > t multiples < T > / / multiplication functor
  4. Template < class T > t divisions < T > / / division imitation function
  5. Template < class T > t module < T > / / take the imitation function
    6) Template < class T > t negate < T > / / take the inverse imitation function, and take the negative sign
/*----------------------------------------------------------------
* Projects: classic question
* Author: Fioman
* Email: geym@hengdingzhineng.com
* Time: March 22, 2022
* Maxim: talk is heap, show me the code^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<functional>
// Built in functor arithmetic functor

// negate 

void test_01(void)
{
	// Reverse
	negate<int> n;
	cout << n(50) << endl;

	// Add, there is only one parameter type, because only those of the same type can be added
	plus<int> p;
	cout << p(10, 20) << endl;

	// reduce
	minus<int> m;
	cout << m(10, 20) << endl;

	// ride
	multiplies<int> mul;
	cout << mul(10, 20) << endl;

	// except
	divides<int> div;
	cout << div(10, 20) << endl;

	// Take mold
	modulus<int> mod;
	cout << mod(10, 20) << endl;
}


int main()
{

	test_01();
	system("pause");
	return 0;
}

⑤ Relational affine function

Function prototype:

  1. template<class T> bool equal_ To < T > / / equal to
  2. template<class T> bool not_ equal_ To < T > / / not equal to
  3. Template < class T > bool greater < T > / / greater than
  4. template<class T> bool greater_ Equal < T > / / greater than or equal to
  5. Template < class T > bool less < T > / / less than
  6. template<class T> bool less_ Equal < T > / / less than or equal to
/*----------------------------------------------------------------
* Projects: classic question
* Author: Fioman
* Email: geym@hengdingzhineng.com
* Time: March 22, 2022
* Maxim: talk is heap, show me the code^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>

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

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

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

	test_01();
	system("pause");
	return 0;
}

⑥ Logic operation imitation function

Prototype:

  1. template<class T> bool logical_ And < T > / / logic and
    2)template<class T> bool logical_ Or < T > / / logical or
    3)template<class T> bool logical_ Not < T > / / not logical
/*----------------------------------------------------------------
* Projects: classic question
* Author: Fioman
* Email: geym@hengdingzhineng.com
* Time: March 22, 2022
* Maxim: talk is heap, show me the code^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<vector>
#include<functional>
#include<algorithm>

void test_01(void)
{
	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;

	// The container v is transported into the container v2 by using logical non and the reverse operation is performed
	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()
{

	test_01();
	system("pause");
	return 0;
}

result:

Tags: C++ C

Posted by deerly on Fri, 15 Apr 2022 14:45:17 +0930