函数对象-C++

打印 上一主题 下一主题

主题 1848|帖子 1848|积分 5544

1.界说


2.特点


1.解释第一句

  1. #include<stdio.h>
  2. using namespace std;
  3. #include<string>
  4. #include<map>
  5. #include <iostream>
  6. class print
  7. {
  8. public:
  9.         void operator()(string s)
  10.         {
  11.                 cout << s << endl;
  12.         }
  13. };
  14. int main()
  15. {
  16.         print print;
  17.         print("hello world");
  18.         return 0;
  19. }
复制代码
2.解释第二句

可以有本身的状态:指的是他宁静凡的函数差别,比方他可以统计本身调用了几次。
也就是说,它可以向函数一样被调用,但是却拥有类的功能,他是类和函数的结合。
  1. #include<stdio.h>
  2. using namespace std;
  3. #include<string>
  4. #include<map>
  5. #include <iostream>
  6. class print
  7. {
  8. public:
  9.         print()
  10.         {
  11.                 this->count = 0;
  12.         }
  13.         void operator()(string s)
  14.         {
  15.                 cout << s << endl;
  16.                 this->count++;
  17.         }
  18.         int count;
  19. };
  20. int main()
  21. {
  22.         print print;
  23.         print("hello world");
  24.         print("hello world");
  25.         print("hello world");
  26.         print("hello world");
  27.         print("hello world");
  28.         cout << print.count << endl;
  29.         return 0;
  30. }
复制代码
3.解释第三句

  1. #include<stdio.h>
  2. using namespace std;
  3. #include<string>
  4. #include<map>
  5. #include <iostream>
  6. class print
  7. {
  8. public:
  9.         print()
  10.         {
  11.                 this->count = 0;
  12.         }
  13.         void operator()(string s)
  14.         {
  15.                 cout << s << endl;
  16.                 this->count++;
  17.         }
  18.         int count;
  19. };
  20. void test(print p, string s)
  21. {
  22.         p(s);
  23. }
  24. void test01()
  25. {
  26.         print print;
  27.         test(print, "C++");
  28. }
  29. int main()
  30. {
  31.         test01();
  32.         return 0;
  33. }
复制代码
函数对象不但可以作为函数参数,还可以再调用的函数中去使用本身重载过的小括号
   具体见
  void test(print p, string s)
{
    p(s);
}

  

3.内建函数对象

1.界说


2.内建函数对象-算数仿函数

1.函数原型


2.取反仿函数(一元仿函数)

  1. #include<stdio.h>
  2. using namespace std;
  3. #include<string>
  4. #include<vector>
  5. #include<functional>
  6. #include <iostream>
  7. int main()
  8. {
  9.         negate<int>n;
  10.         cout << n(10) << endl;
  11.         return 0;
  12. }
复制代码


3.加法仿函数(二元仿函数)

  1. #include<stdio.h>
  2. using namespace std;
  3. #include<string>
  4. #include<vector>
  5. #include<functional>
  6. #include <iostream>
  7. int main()
  8. {
  9.         plus<int>n;
  10.         cout << n(10,20) << endl;
  11.         return 0;
  12. }
复制代码


注意:plus模板参数列表中只须要有一个int就行,是为了防止出现两个差别范例的数据相加
4.内建函数对象-关系仿函数

1.函数原型


2.代码

  1. #include<stdio.h>
  2. using namespace std;
  3. #include<string>
  4. #include<vector>
  5. #include<functional>
  6. #include<algorithm>
  7. #include <iostream>
  8. int main()
  9. {
  10.         vector<int> v;
  11.         v.push_back(1);
  12.         v.push_back(5);
  13.         v.push_back(3);
  14.         v.push_back(2);
  15.         sort(v.begin(), v.end(), greater<int>());
  16.         for (int i = 0; i < v.size(); i++)
  17.         {
  18.                 cout << v[i] << endl;
  19.         }
  20.         return 0;
  21. }
复制代码
5.内建函数对象-逻辑仿函数

1.函数原型


2.代码

  1. #include<stdio.h>
  2. using namespace std;
  3. #include<string>
  4. #include<vector>
  5. #include<functional>
  6. #include<algorithm>
  7. #include <iostream>
  8. int main()
  9. {
  10.         vector<int> v;
  11.         v.push_back(1);
  12.         v.push_back(5);
  13.         v.push_back(3);
  14.         v.push_back(2);
  15.         vector<int> v1;
  16.         v1.resize(v.size());
  17.         transform(v.begin(), v.end(),v1.begin(), logical_not<bool>());
  18.         for (int i = 0; i < v.size(); i++)
  19.         {
  20.                 cout << v[i] << " ";
  21.         }
  22.         cout << endl;
  23.         for (int i = 0; i < v1.size(); i++)
  24.         {
  25.                 cout << v1[i] << " ";
  26.         }
  27.         cout << endl;
  28.         return 0;
  29. }
复制代码

   其中用到了搬运函数transform,须要提前给他开发相应的空间
      v1.resize(v.size());
    transform(v.begin(), v.end(),v1.begin(), logical_not<bool>());

  4.注意

逻辑仿函数现实应用较少,相识即可

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立山

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表