1 函数对象

1.1 函数对象概念

概念:

  • 重载函数调用操作符的类,其对象常称为函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:
函数对象(仿函数)是一个,不是一个函数

1.2 函数对象使用

仿函数写法非常灵活,可以作为参数进行传递
特点:

  • 函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <string>
  4. //1、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
  5. class MyAdd
  6. {
  7. public:
  8. int operator()(int v1, int v2)
  9. {
  10. return v1 + v2;
  11. }
  12. };
  13. void test01()
  14. {
  15. MyAdd myAdd;
  16. cout << myAdd(10, 10) << endl;
  17. }
  18. //2、函数对象可以有自己的状态
  19. class MyPrint
  20. {
  21. public:
  22. MyPrint()
  23. {
  24. count = 0;
  25. }
  26. void operator()(string test)
  27. {
  28. cout << test << endl;
  29. count++; //统计使用次数
  30. }
  31. int count; //内部自己的状态
  32. };
  33. void test02()
  34. {
  35. MyPrint myPrint;
  36. myPrint("hello world");
  37. myPrint("hello world");
  38. myPrint("hello world");
  39. cout << "myPrint调用次数为: " << myPrint.count << endl;
  40. }
  41. //3、函数对象可以作为参数传递
  42. void doPrint(MyPrint& mp, string test)
  43. {
  44. mp(test);
  45. }
  46. void test03()
  47. {
  48. MyPrint myPrint;
  49. doPrint(myPrint, "Hello C++");
  50. }
  51. int main() {
  52. test01();
  53. test02();
  54. test03();
  55. return 0;
  56. }
  57. /* 输出结果
  58. 20
  59. hello world
  60. hello world
  61. hello world
  62. myPrint调用次数为: 3
  63. Hello C++
  64. */

2 谓词

2.1 谓词概念

概念:

  • 返回bool类型的仿函数称为谓词
  • 如果operator()接受一个参数,那么叫做一元谓词
  • 如果operator()接受两个参数,那么叫做二元谓词

2.2 一元谓词

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <algorithm>
  5. //1.一元谓词
  6. struct GreaterFive {
  7. bool operator()(int val) {
  8. return val > 5;
  9. }
  10. };
  11. void test01() {
  12. vector<int> v;
  13. for (int i = 0; i < 10; i++)
  14. {
  15. v.push_back(i);
  16. }
  17. vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
  18. if (it == v.end()) {
  19. cout << "没找到!" << endl;
  20. }
  21. else {
  22. cout << "找到:" << *it << endl;
  23. }
  24. }
  25. int main() {
  26. test01();
  27. return 0;
  28. }
  29. /* 输出结果
  30. 找到:6
  31. */

2.3 二元谓词

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <algorithm>
  5. //二元谓词
  6. class MyCompare
  7. {
  8. public:
  9. bool operator()(int num1, int num2)
  10. {
  11. return num1 > num2;
  12. }
  13. };
  14. void test01()
  15. {
  16. vector<int> v;
  17. v.push_back(10);
  18. v.push_back(40);
  19. v.push_back(20);
  20. v.push_back(30);
  21. v.push_back(50);
  22. //默认从小到大
  23. sort(v.begin(), v.end());
  24. for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  25. {
  26. cout << *it << " ";
  27. }
  28. cout << endl;
  29. cout << "----------------------------" << endl;
  30. //使用函数对象改变算法策略,排序从大到小
  31. sort(v.begin(), v.end(), MyCompare());
  32. for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
  33. {
  34. cout << *it << " ";
  35. }
  36. cout << endl;
  37. }
  38. int main() {
  39. test01();
  40. return 0;
  41. }
  42. /* 输出结果
  43. 10 20 30 40 50
  44. ----------------------------
  45. 50 40 30 20 10
  46. */

3 内建函数对象

3.1 内建函数对象意义

概念:

  • STL内建了一些函数对象

分类:

  • 算术仿函数
  • 关系仿函数
  • 逻辑仿函数

用法:

  • 这些仿函数所产生的对象,用法和一般函数完全相同
  • 使用内建函数对象,需要引入头文件 #include<functional>

3.2 算术仿函数

功能描述:

  • 实现四则运算
  • 其中negate是一元运算,其他都是二元运算

仿函数原型:

  1. template<class T> T plus<T> //加法仿函数
  2. template<class T> T minus<T> //减法仿函数
  3. template<class T> T multiplies<T> //乘法仿函数
  4. template<class T> T divides<T> //除法仿函数
  5. template<class T> T modulus<T> //取模仿函数
  6. template<class T> T negate<T> //取反仿函数

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <functional>
  4. //negate
  5. void test01()
  6. {
  7. negate<int> n;
  8. cout << n(50) << endl;
  9. }
  10. //plus
  11. void test02()
  12. {
  13. plus<int> p;
  14. cout << p(10, 20) << endl;
  15. }
  16. int main() {
  17. test01();
  18. test02();
  19. return 0;
  20. }
  21. /* 输出结果
  22. -50
  23. 30
  24. */

3.3 关系仿函数

功能描述:

  • 实现关系对比

仿函数原型:

  1. template<class T> bool equal_to<T> //等于
  2. template<class T> bool not_equal_to<T> //不等于
  3. template<class T> bool greater<T> //大于
  4. template<class T> bool greater_equal<T> //大于等于
  5. template<class T> bool less<T> //小于
  6. template<class T> bool less_equal<T> //小于等于

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <functional>
  4. #include <vector>
  5. #include <algorithm>
  6. class MyCompare
  7. {
  8. public:
  9. bool operator()(int v1, int v2)
  10. {
  11. return v1 > v2;
  12. }
  13. };
  14. void test01()
  15. {
  16. vector<int> v;
  17. v.push_back(10);
  18. v.push_back(30);
  19. v.push_back(50);
  20. v.push_back(40);
  21. v.push_back(20);
  22. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  23. cout << *it << " ";
  24. }
  25. cout << endl;
  26. //自己实现仿函数
  27. //sort(v.begin(), v.end(), MyCompare());
  28. //STL内建仿函数 大于仿函数
  29. sort(v.begin(), v.end(), greater<int>());
  30. for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
  31. cout << *it << " ";
  32. }
  33. cout << endl;
  34. }
  35. int main() {
  36. test01();
  37. return 0;
  38. }
  39. /* 输出结果
  40. 10 30 50 40 20
  41. 50 40 30 20 10
  42. */

3.4 逻辑仿函数

函数原型:

  1. template<class T> bool logical_and<T> //逻辑与
  2. template<class T> bool logical_or<T> //逻辑或
  3. template<class T> bool logical_not<T> //逻辑非

示例:

  1. #include<iostream>
  2. using namespace std;
  3. #include <vector>
  4. #include <functional>
  5. #include <algorithm>
  6. void test01()
  7. {
  8. vector<bool> v;
  9. v.push_back(true);
  10. v.push_back(false);
  11. v.push_back(true);
  12. v.push_back(false);
  13. for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
  14. {
  15. cout << *it << " ";
  16. }
  17. cout << endl;
  18. //逻辑非 将v容器搬运到v2中,并执行逻辑非运算
  19. vector<bool> v2;
  20. v2.resize(v.size());
  21. transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
  22. for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
  23. {
  24. cout << *it << " ";
  25. }
  26. cout << endl;
  27. }
  28. int main() {
  29. test01();
  30. return 0;
  31. }
  32. /* 输出结果
  33. 1 0 1 0
  34. 0 1 0 1
  35. */

示例:

  1. /* 输出结果
  2. */

示例:

  1. /* 输出结果
  2. */