函数对象基本概念
- 重载函数调用操作符的类,其对象常称为函数对象
- 函数对象使用重载的 () 时,行为类似函数调用,也叫仿函数
- 函数对象是一个类的对象,不是一个函数
函数对象使用
- 函数对象在使用时,可以向普通函数那样调用,有参数,有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
// 函数对象可以像普通函数那样调用,有参数,有返回值
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void functor()
{
MyAdd myAdd;
cout << myAdd(10, 10) << endl;
}
// 函数对象可以有自己的状态
class MyPrint
{
public:
int count = 0;
void operator()(string test)
{
cout << test << endl;
count++;
}
};
void functor_state()
{
MyPrint myPrint;
myPrint("Hello world");
myPrint("Hello world");
myPrint("Hello world");
myPrint("Hello world");
myPrint("Hello world");
cout << myPrint.count << endl;
}
// 函数对象可以作为参数传递
void doPrint(MyPrint& mp, string text)
{
mp(text);
}
void functor_asParameter()
{
MyPrint myPrint;
doPrint(myPrint, "hello C++");
}
谓词(predicate)
- 返回 bool 类型的仿函数称为谓词
- 如果 operator() 接收一个参数,那么就叫做一元谓词
- 如果 operator() 接收两个参数,那么就叫做二元谓词
一元谓词
iterator find_if( v.begin(), v.end(), binary_pred );
// 一元谓词
class GT5
{
public:
bool operator()(int val)
{
return val > 5;
}
};
void predicate_unary()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// 查找容器中有没有 >5 的数字
auto it = find_if(v.begin(), v.end(), GT5());
if (it == v.end())
{
cout << "未找到" << endl;
}
else
{
cout << "找到的值为:" << *it << endl;
}
}
二元谓词
void sort( v.begin(), v.end(), binary_pred );
// 二元谓词
class MyCompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void predicate_binary()
{
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(20);
v.push_back(30);
v.push_back(50);
sort(v.begin(), v.end());
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout << "---------------------------" << endl;
sort(v.begin(), v.end(), MyCompare());
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
内建函数对象
- STL 内建了一些函数对象,使用时要引入头文件 functional
- 算术仿函数
- 关系仿函数
- 逻辑仿函数
- 用法和一般函数完全相同
算术仿函数
- 实现四则运算
- negate 是一元运算,其它都是二元运算
T plus<T>
T minus<T>
T multiplies<T>
T divides<T>
T modulus<T>
T negate<T>
// 内建函数对象:算数仿函数
void negate_example()
{
negate<int> n;
cout << n(50) << endl;
}
void plus_example()
{
plus<int> p;
cout << p(1, 2) << endl;
}
关系仿函数
bool equal_to<T>
bool not_equal_to<T>
bool greater<T>
bool greater_equal<T>
bool less<T>
bool less_equal<T>
// 内建函数对象:关系仿函数
void greater_example()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(40);
v.push_back(20);
v.push_back(50);
v.push_back(60);
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
sort(v.begin(), v.end(), greater<int>());
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
逻辑仿函数
bool logical_and<T>
bool logical_or<T>
bool logical_not<T>
iterator transform( v1.begin(), v1.end(), v2.begin(), logical_not<bool>() );
- 注意:v2 要提前 resize 到 v1 的大小
// 内建函数对象:逻辑仿函数
void logical_not_example()
{
vector<bool> v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
for (auto it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
vector<bool> v2;
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
for (auto it = v2.begin(); it != v2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}