- 重载 函数调用操作符 的类,其对象常称为函数对象
- 函数对象 使用重载的()时,行为类似函数调用,也叫仿函数
- 本质:函数对象(仿函数)是一个类,不是一个函数
特点:
- 函数对象可以像普通函数那样调用,可以有参数,可以有返回值
- 函数对象超出普通函数的概念,可以有自己的状态
- 函数对象可以作为参数传递
简单示例:
class MyPrint{
public:
MyPrint() {
this->count = 0;
}
int operator()(std::string str) {
std::cout << str << std::endl;
count++;
}
int count;
};
void doPrint(MyPrint & mp,std::string str){
mp(str);
}
// 测试函数对象的使用
void testFuncObj() {
MyPrint myPrint;
doPrint(myPrint,"调用函数");
doPrint(myPrint,"调用函数");
std::cout << "函数调用次数:"<< myPrint.count<< std::endl;
}
int main() {
testFuncObj();
return 0;
}
打印结果:
调用函数 调用函数 函数调用次数:22. 谓词 谓词概念
- 返回bool类型的仿函数称为谓词
- 如果operator()接受一个参数,那么叫一元谓词
- 如果operator()接受两个参数,那么叫二元谓词
一元谓词:
class Pred{
public:
bool operator()(int num) {
return num>5;
}
};
void testPred1() {
std::vector vector;
for (int i = 0; i < 10; ++i) {
vector.push_back(i);
}
for (std::vector::iterator it = std::find_if(vector.begin(),vector.end(),Pred()); it != vector.end(); it++) {
std::cout << "找到大于5的数字:" << *it <
输出结果:
找到大于5的数字:6
找到大于5的数字:7
找到大于5的数字:8
找到大于5的数字:9
二元谓词:
// 测试谓词的使用
class Pred{
public:
bool operator()(int num) {
return num>5;
}
bool operator()(int val1, int val2) {
return val1 > val2;
}
};
void testPred2() {
std::vector vector;
vector.push_back(10);
vector.push_back(-2);
vector.push_back(80);
vector.push_back(1);
vector.push_back(104);
vector.push_back(23);
vector.push_back(67);
// std::sort(vector.begin(),vector.end()); // 默认是升序
std::sort(vector.begin(),vector.end(),Pred()); // 通过二元谓词改为降序
for (std::vector::iterator it = vector.begin(); it != vector.end() ; it++) {
std::cout << *it << std::endl;
}
}
int main() {
testPred2(); // 二元谓词
return 0;
}
3. 内建函数对象
STL内建了一些函数对象,这些仿函数所产生的对象,用法和一般函数完全相同,使用内建函数对象需要引入头文件:`#include ` 分类如下:
- 算术仿函数
- 关系仿函数
- 逻辑仿函数
算术仿函数
实现四则运算,其中negate是一元运算,其它都是二元运算
仿函数原型
- template
T plus // 加法仿函数 - template
T minus // 减法仿函数 - template
T multiplies // 乘法仿函数 - template
T divides // 加法仿函数 - template
T modulus // 减法仿函数 - template
T negate // 乘法仿函数
算数仿函数使用示例
void testNegate() {
std::negate n;
std::cout << n(50) << std::endl;
}
void testAdd() {
std::plus n;
std::cout << n(10,20) <
运行结果
-50
30
关系仿函数
实现关系对比
仿函数原型
- template
bool equal_to // 等于 - template
bool not_equal_to // 不等于 - template
bool greater // 大于 - template
bool greater_equal // 大于等于 - template
bool less // 小于 - template
bool less_equal // 小于等于
使用示例
void testGreater() {
std::vector v;
v.push_back(10);
v.push_back(20);
v.push_back(-10);
v.push_back(3);
v.push_back(109);
v.push_back(-66);
// std::sort(v.begin(), v.end()); // 原来的sort是升序排列
std::sort(v.begin(),v.end(),std::greater()); // 降序排列
for (std::vector::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << std::endl;
}
}
int main() {
testGreater(); // 内建函数关系仿函数,大于关系
return 0;
}
输出:
-66
-10
3
10
20
109
逻辑仿函数
实现逻辑运算
函数原型
- template
bool logical_and 逻辑与 - template
bool logical_or 逻辑或 - template
bool logical_not 逻辑非
使用示例
void testLogical() {
std::vector v;
v.push_back(true);
v.push_back(false);
v.push_back(true);
v.push_back(false);
std::vector v2;
v2.resize(v.size());
std::transform(v.begin(),v.end(),v2.begin(),std::logical_not());
for (std::vector::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
for (std::vector::iterator it = v2.begin(); it != v2.end(); ++it) {
std::cout << *it << " ";
}
}
int main() {
testLogical(); // 内建函数逻辑仿函数,逻辑非
return 0;
}
输出:
1 0 1 0
0 1 0 1



