1.界说
2.特点
、
1.解释第一句
- #include<stdio.h>
- using namespace std;
- #include<string>
- #include<map>
- #include <iostream>
- class print
- {
- public:
- void operator()(string s)
- {
- cout << s << endl;
- }
- };
- int main()
- {
- print print;
- print("hello world");
- return 0;
- }
复制代码 2.解释第二句
可以有本身的状态:指的是他宁静凡的函数差别,比方他可以统计本身调用了几次。
也就是说,它可以向函数一样被调用,但是却拥有类的功能,他是类和函数的结合。
- #include<stdio.h>
- using namespace std;
- #include<string>
- #include<map>
- #include <iostream>
- class print
- {
- public:
- print()
- {
- this->count = 0;
- }
- void operator()(string s)
- {
- cout << s << endl;
- this->count++;
- }
- int count;
- };
- int main()
- {
- print print;
- print("hello world");
- print("hello world");
- print("hello world");
- print("hello world");
- print("hello world");
- cout << print.count << endl;
- return 0;
- }
复制代码 3.解释第三句
- #include<stdio.h>
- using namespace std;
- #include<string>
- #include<map>
- #include <iostream>
- class print
- {
- public:
- print()
- {
- this->count = 0;
- }
- void operator()(string s)
- {
- cout << s << endl;
- this->count++;
- }
- int count;
- };
- void test(print p, string s)
- {
- p(s);
- }
- void test01()
- {
- print print;
- test(print, "C++");
- }
- int main()
- {
- test01();
- return 0;
- }
复制代码 函数对象不但可以作为函数参数,还可以再调用的函数中去使用本身重载过的小括号
具体见
void test(print p, string s)
{
p(s);
}
3.内建函数对象
1.界说
2.内建函数对象-算数仿函数
1.函数原型
2.取反仿函数(一元仿函数)
- #include<stdio.h>
- using namespace std;
- #include<string>
- #include<vector>
- #include<functional>
- #include <iostream>
- int main()
- {
- negate<int>n;
- cout << n(10) << endl;
- return 0;
- }
复制代码
3.加法仿函数(二元仿函数)
- #include<stdio.h>
- using namespace std;
- #include<string>
- #include<vector>
- #include<functional>
- #include <iostream>
- int main()
- {
- plus<int>n;
- cout << n(10,20) << endl;
- return 0;
- }
复制代码
注意:plus模板参数列表中只须要有一个int就行,是为了防止出现两个差别范例的数据相加
4.内建函数对象-关系仿函数
1.函数原型
2.代码
- #include<stdio.h>
- using namespace std;
- #include<string>
- #include<vector>
- #include<functional>
- #include<algorithm>
- #include <iostream>
- int main()
- {
- vector<int> v;
- v.push_back(1);
- v.push_back(5);
- v.push_back(3);
- v.push_back(2);
- sort(v.begin(), v.end(), greater<int>());
- for (int i = 0; i < v.size(); i++)
- {
- cout << v[i] << endl;
- }
- return 0;
- }
复制代码 5.内建函数对象-逻辑仿函数
1.函数原型
2.代码
- #include<stdio.h>
- using namespace std;
- #include<string>
- #include<vector>
- #include<functional>
- #include<algorithm>
- #include <iostream>
- int main()
- {
- vector<int> v;
- v.push_back(1);
- v.push_back(5);
- v.push_back(3);
- v.push_back(2);
- vector<int> v1;
- v1.resize(v.size());
- transform(v.begin(), v.end(),v1.begin(), logical_not<bool>());
- for (int i = 0; i < v.size(); i++)
- {
- cout << v[i] << " ";
- }
- cout << endl;
- for (int i = 0; i < v1.size(); i++)
- {
- cout << v1[i] << " ";
- }
- cout << endl;
- return 0;
- }
复制代码
其中用到了搬运函数transform,须要提前给他开发相应的空间
v1.resize(v.size());
transform(v.begin(), v.end(),v1.begin(), logical_not<bool>());
4.注意
逻辑仿函数现实应用较少,相识即可
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |