STL - function object
The concept of function objects
concept:
- A class that overloads the function call operator, its object is often called a function object
- When a function object uses overloaded (), it behaves like a function call, also called a functor
Essence:
Function object (functor) is a class, not a function
Use of function objects
Features:
- When a function object is used, it can be called like a normal function, it can have parameters, and it can have a return value
- Function objects go beyond the concept of ordinary functions, and function objects can have their own state
- Function objects can be passed as arguments
Example:
#include<iostream>
using namespace std;
//函数对象
/*
-函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
*/
class MyAdd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
//1、函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
void _01Test01()
{
MyAdd add;
cout << add(10, 20) << endl;;
}
//2、函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint
{
public:
//记录MyPrint被调用的次数
int count;
public:
MyPrint()
{
count = 0;
}
void operator()(string str)
{
cout << str << endl;
count++;
}
};
void _01Test02()
{
MyPrint print;
print("hello");
print("hello");
print("hello");
print("hello");
print("hello");
cout << "print被调用的次数:" << print.count << endl;
}
//3、函数对象可以作为参数传递
void doPrint(MyPrint& print, string str)
{
print(str);
}
void _01Test03()
{
MyPrint print;
string str = "hello world";
doPrint(print, str);
}
void main()
{
//_01Test01();
//_01Test02();
_01Test03();
}
predicate concept
concept:
- Functors that return bool type are called predicates
- If operator() takes one argument, it is called a unary predicate
- If operator() takes two arguments, it is called a binary predicate
Example:
unary predicate:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//一元谓词 operator()参数只有一个
class GreaterFive
{
public:
bool operator()(int num)
{
return num > 5;
}
};
void _Test02()
{
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 << *it << endl;
}
else
{
cout << "没有大于5的数" << endl;
}
}
void main()
{
_Test02();
}
Binary predicates:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//二元谓词
class MyCompare
{
public:
bool operator()(int num1, int num2)
{
return num1 > num2;
}
};
void _03Test01()
{
vector<int>v;
v.push_back(10);
v.push_back(40);
v.push_back(30);
v.push_back(50);
v.push_back(20);
sort(v.begin(), v.end());
for (vector<int>::iterator it = v.begin();it!= v.end(); it++)
{
cout << *it << endl;
}
cout << "------------------------" << endl;
sort(v.begin(), v.end(), MyCompare());
for (vector<int>::iterator it = v.begin(); it!=v.end(); it++)
{
cout << *it << endl;
}
}
void main()
{
_03Test01();
}
built-in function object
concept:
- The STL has some built-in function objects
Classification:
- arithmetic functor
- relational functor
- logical functor
usage:
- The objects generated by these functors are used in exactly the same way as ordinary functions
- To use the built-in function object, you need to introduce the header file #include
arithmetic functor
Function:
- Realize the four operations
- Where negate is a unary operation. Everything else is a binary operation
Functor 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<iostream>
#include<functional>
using namespace std;
//内建函数对象 算术仿函数
//取反仿函数
void _04Test01()
{
negate<int> qufan;
cout << qufan(50) << endl;
}
//加法仿函数
void _04Test02()
{
plus<int> add;
cout << add(10, 20) << endl;
}
//减法仿函数
void _04Test03()
{
minus<int> min;
cout << min(10, 20) << endl;
}
void main()
{
//_04Test01();
//_04Test02();
_04Test03();
}
relational functor
Function:
- Realize relational comparison
Functor prototype:
- template< class T > bool equal_to< T > //等于
- template< class T > bool not_equal_to< T > // not equal
- template< class T > bool greater< T > //大于
- template< class T > bool greater_equal< T > // greater than or equal to
- template< class T > bool less< T > //小于
- template< class T > bool less_greater< T > //less than or equal to
logical functor
Function:
- Implement logical operations
Function prototype:
- template< class T > bool logical_and //logical and
- template< class T > bool logical_or //logical or
- template< class T > bool logical_not // logical not