C++ Development of Generic Programming Lecture 13 [Built-in Function Objects]

Preface

concept:

STL has some built-in function objects. such as:

classification:

  • Arithmetic functor

  • Relation functor

  • Logical functor

usage:

  • The usage of the objects produced by these functors is exactly the same as that of general functions
  • Use the built-in function object, you need to introduce the header file #include<functional>

One, arithmetic functor

Function description:

  • Realize the four arithmetic
  • Negate is a unary operation, and the others are binary operations

Function prototype:

  • template<class T> T plus<T> //Addition functor
  • template<class T> T minus<T> //Subtraction functor
  • template<class T> T multiplies<T> //Multiplication functor
  • template<class T> T divides<T> //Division functor
  • template<class T> T modulus<T> //Take the imitation function
  • template<class T> T negate<T> //Reverse functor

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 the built-in function object, you need to introduce the header file #include <functional>

Second, the relational functor

Function description:

  • Realize relationship comparison

Function prototype:

  • template<class T> bool equal_to<T> //equal
  • template<class T> bool not_equal_to<T> //not equal to
  • template<class T> bool greater<T> //more than the
  • template<class T> bool greater_equal<T> //greater or equal to
  • template<class T> bool less<T> //Less than
  • template<class T> bool less_equal<T> //Less than or equal

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;

	//自己实现仿函数
	//sort(v.begin(), v.end(), MyCompare());
	//STL内建仿函数  大于仿函数
	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: The most commonly used relational functor is greater<> greater than

Three, logical functors

Function description:

  • Realize logical operations

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> //Logical negation

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;

	//逻辑非  将v容器搬运到v2中,并执行逻辑非运算
	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;
}

Summary: The actual application of logical functors is less, just understand

Guess you like

Origin blog.csdn.net/Kukeoo/article/details/114108503