重载了函数调用操作符的类,其对象称为函数对象。特别地,函数对象使用重载的
()
时,行为类似函数调用,也叫仿函数。
#include<iostream>
#include<string>
using namespace std;
class MyAdd
{
public:
int operator()(const int a, const int b)
{
return a + b;
}
};
class MyPrint
{
public:
int count; //记录调用次数
public:
MyPrint()
{
this->count = 0; //初始化调用次数为0
}
void operator()(const string str)
{
cout << str << endl;
this->count++; //调用次数加1
}
};
void doPrint(MyPrint& mp, string str)
{
mp(str);
}
int main()
{
MyAdd myAdd; //创建函数对象
cout << myAdd(10, 20) << endl; //(1)函数对象可以像普通函数那样调用,可以有参数和返回值
MyPrint myPrint;
myPrint("Hello!");
myPrint("Hello!");
myPrint("Hello!");
cout << "myPrint的调用次数为:" << myPrint.count << endl; //(2)函数对象可以有自己内部的状态(输出3)
doPrint(myPrint, "Hello!"); //(3)函数对象可以作为参数进行传递
system("pause");
return 0;
}
(16.1) 谓词
返回
bool
类型的仿函数称为谓词。如果operator()
接受一个参数,那么叫做一元谓词;如果operator()
接受两个参数,那么叫做二元谓词。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GtFive //一元谓词
{
public:
bool operator()(const int a)
{
return a > 5;
}
};
class MyCompare //二元谓词
{
public:
bool operator()(const int a,const int b)
{
return a > b; //降序
}
};
int main()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//find_if算法第三个参数是谓词,GtFive()是匿名函数对象
vector<int>::iterator it = find_if(v.begin(), v.end(), GtFive()); //查找容器中大于5的数,返回迭代器
if (it == v.end())
{
cout << "没有找到。" << endl;
}
else
{
cout << *it << endl; //6
}
sort(v.begin(), v.end()); //默认升序排列
//sort算法的第三个参数也是谓词,MyCompare()是匿名函数对象
sort(v.begin(), v.end(), MyCompare()); //使用函数对象改变排序策略
system("pause");
return 0;
}
(16.2) 算术仿函数
STL也提供了一些仿函数,这些仿函数被称为内建函数对象。有算术仿函数、关系仿函数、逻辑仿函数。需要包含头文件
#include<functional>
。
#include<iostream>
#include<functional>
using namespace std;
int main()
{
//取相反数仿函数
negate<int> n;
cout << n(10) << endl; //-10
//取余仿函数
modulus<int> md;
cout << md(13, 5) << endl;; //3
//加法仿函数
plus<int> p;
cout << p(10, 20) << endl; //30
//减法仿函数
minus<int> m;
cout << m(30, 10) << endl; //20
//乘法仿函数
multiplies<int> ml;
cout << ml(20, 30) << endl; //600
//除法仿函数
divides<int> d;
cout << d(100, 10) << endl; //10
system("pause");
return 0;
}
(16.3) 关系仿函数
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
v.push_back(20);
v.push_back(10);
v.push_back(30);
sort(v.begin(), v.end(), greater<int>()); //用STL提供的关系仿函数,实现降序排序
//其他STL关系仿函数:
//equal_to<T>(); //等于
//not_equal_to<T>(); //不等于
//greater<T>(); //大于
//less<T>(); //小于
//greater_equal<T>(); //大于等于
//less_equal<T>(); //小于等于
system("pause");
return 0;
}
(16.4) 逻辑仿函数
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
vector<bool> v1;
v1.push_back(true);
v1.push_back(false);
vector<bool> v2;
v2.resize(v1.size());
transform(v1.begin(), v1.end(), v2.begin(), logical_not<bool>()); //将v1搬运到v2中并取反
//其他STL逻辑仿函数:
//logical_and<T>(); //与
//logical_or<T>(); //或
//logical_not<T>(); //非
system("pause");
return 0;
}
《 C + + S Y N T A X 》 系 列 博 客 创 作 参 考 资 料 来 源 《C++\ SYNTAX》系列博客创作参考资料来源 《C++ SYNTAX》系列博客创作参考资料来源
- 《黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难》@黑马程序员.https://www.bilibili.com.
博 客 创 作 : A i d e n L e e 博客创作:Aiden\ Lee 博客创作:Aiden Lee
特别声明:文章仅供学习参考,转载请注明出处,严禁盗用!