• C++笔记打卡第22天(仿函数)


    1.函数对象

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

    使用时的特点:

    • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
    • 函数对象超出普通函数的概念,函数对象可以有自己的状态
    • 函数对象可以作为参数传递
    1. // 1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
    2. class MyAdd
    3. {
    4. public:
    5. int operator()(int v1, int v2)
    6. {
    7. return v1 + v2;
    8. }
    9. };
    10. void test01()
    11. {
    12. MyAdd myadd;
    13. cout << myadd(10, 20) << endl;
    14. }
    15. // 2.函数对象超出普通函数的概念,函数对象可以有自己的状态
    16. class MyPrint
    17. {
    18. public:
    19. MyPrint()
    20. {
    21. this->count = 0;
    22. }
    23. void operator()(string text)
    24. {
    25. cout << text << endl;
    26. this->count++;
    27. }
    28. int count; // 记录调用次数
    29. };
    30. void test02()
    31. {
    32. MyPrint mp;
    33. mp("Hello");
    34. cout << mp.count << endl;
    35. }
    36. // 3.函数对象可以作为参数传递
    37. void doPrint(MyPrint &mp, string text)
    38. {
    39. mp(text);
    40. }
    41. void test03()
    42. {
    43. MyPrint mp;
    44. doPrint(mp, "hello");
    45. }

    2.谓词

    • 返回bool类型的仿函数称为谓词
    • 如果operator()接受一个参数,叫做一元谓词
    • 如果operator()接受两个参数,叫做二元谓词
    1. // 一元谓词
    2. class GreaterFive
    3. {
    4. public:
    5. bool operator()(int num)
    6. {
    7. return num > 5;
    8. }
    9. };
    10. void test01()
    11. {
    12. vector<int> v;
    13. for(int i=0; i<10; i++)
    14. {
    15. v.push_back(i);
    16. }
    17. // 查找容器中是否有大于5的数字
    18. // GreaterFive()是一个匿名的函数对象
    19. // 返回值是一个迭代器
    20. vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
    21. if(it == v.end())
    22. {
    23. cout << "未找到" << endl;
    24. }
    25. else
    26. {
    27. cout << "找到" << *it << endl;
    28. }
    29. }
    1. // 二元谓词
    2. class MyCompare
    3. {
    4. public:
    5. bool operator()(int num1, int num2)
    6. {
    7. return num1 > num2;
    8. }
    9. };
    10. void test01()
    11. {
    12. vector<int> v;
    13. v.push_back(10);
    14. v.push_back(30);
    15. v.push_back(20);
    16. v.push_back(60);
    17. v.push_back(50);
    18. sort(v.begin(), v.end());
    19. for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
    20. {
    21. cout << *it << " ";
    22. }
    23. cout << endl;
    24. // 使用仿函数,可以改变排序规则为降序
    25. sort(v.begin(), v.end(), MyCompare());
    26. for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
    27. {
    28. cout << *it << " ";
    29. }
    30. cout << endl;
    31. }

    3.内建函数

    • STL内建了一些函数对象(仿函数)

    分类:

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

    用法:

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

    4.算术仿函数

    • template T plus; 加法仿函数
    • template T minus; 减法仿函数
    • template T multiplies; 乘法仿函数
    • template T divides; 除法仿函数
    • template T modulus; 取模仿函数
    • template T negate; 取反仿函数
    1. #include
    2. // negate 一元仿函数 取反仿函数
    3. void test01()
    4. {
    5. negate<int> n;
    6. cout << n(50) << endl; // -50
    7. }
    8. // plus 二元仿函数 加法
    9. void test02()
    10. {
    11. plus<int> p;
    12. cout << p(10, 20) << endl;
    13. }

    5.关系仿函数

    • template bool equal_to #等于
    • template bool not_equal_to #不等于
    • template bool greater 大于
    • template bool greater_equal_ 大于等于
    • template bool less 小于
    • template bool less_equal 小于等于
    1. #include
    2. class MyCompare
    3. {
    4. public:
    5. bool operator()(int num1, int num2)
    6. {
    7. return num1 > num2;
    8. }
    9. };
    10. void test01()
    11. {
    12. vector<int> v;
    13. v.push_back(10);
    14. v.push_back(30);
    15. v.push_back(40);
    16. v.push_back(20);
    17. v.push_back(50);
    18. for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
    19. {
    20. cout << *it << " ";
    21. }
    22. cout << endl;
    23. // 降序
    24. // 等价于sort(v.begin(), v.end(), MyCompare());
    25. // greater()是内建函数对象(仿函数)
    26. sort(v.begin(), v.end(), greater<int>());
    27. for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
    28. {
    29. cout << *it << " ";
    30. }
    31. cout << endl;
    32. }

    6.逻辑仿函数

    • template bool logical_and; 逻辑与
    • template bool logical_or; 逻辑或
    • template bool logical_not; 逻辑非
    1. void test01()
    2. {
    3. vector<bool> v;
    4. v.push_back(true);
    5. v.push_back(false);
    6. v.push_back(true);
    7. v.push_back(false);
    8. for(vector<bool>::iterator it=v.begin(); it!=v.end(); it++)
    9. {
    10. cout << *it << " ";
    11. }
    12. cout << endl; // 1 0 1 0
    13. // 利用逻辑非 将容器v搬运到容器v2中,并执行取反操作
    14. vector<bool> v2;
    15. v2.resize(v.size()); // 必须提前开辟空间,指定大小
    16. transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
    17. for(vector<bool>::iterator it=v2.begin(); it!=v2.end(); it++)
    18. {
    19. cout << *it << " ";
    20. }
    21. cout << endl; // 0 1 0 1
    22. }

    7.STL常用算法

    • 算法主要是由头文件组成
    • 是所有STL文件中最大的一个,范围涉及到比较、交换、查找、遍历、复制、修改等等
    • 体积很小,只包括几个在序列上面进行简单数学运算的模版函数
    • 定义了一些模版类,用以声明函数对象

    8.常用遍历算法

    • for_each:遍历容器

      1. // 普通函数
      2. void print01(int val)
      3. {
      4. cout << val << endl;
      5. }
      6. // 仿函数
      7. class print02
      8. {
      9. public:
      10. void operator()(int val)
      11. {
      12. cout << val << endl;
      13. }
      14. };
      15. void test01()
      16. {
      17. vector<int> v;
      18. for(int i=0; i<10; i++)
      19. {
      20. v.push_back(i);
      21. }
      22. for_each(v.begin(), v.end(), print01);
      23. cout << endl;
      24. for_each(v.begin(), v.end(), print02());
      25. cout << endl;
      26. }
    • transform:搬运容器到另一个容器中

      1. class Transform
      2. {
      3. public:
      4. int operator()(int val)
      5. {
      6. return val;
      7. }
      8. };
      9. class Myprint
      10. {
      11. public:
      12. void operator()(int val)
      13. {
      14. cout << val << " ";
      15. }
      16. };
      17. void test01()
      18. {
      19. vector<int> v;
      20. for(int i=0; i<10; i++)
      21. {
      22. v.push_back(i);
      23. }
      24. vector<int> v2; // 目标容器
      25. v2.resize(v.size()); // 目标容器需要提前开辟空间
      26. transform(v.begin(), v.end(), v2.begin(), Transform());
      27. for_each(v2.begin(), v2.end(), Myprint());
      28. cout << endl;
      29. }

    9.常用查找算法

    • find:查找元素**(返回值是一个迭代器)**

      1. // 查内置数据类型
      2. void test01()
      3. {
      4. vector<int> v;
      5. for(int i=0; i<10; i++)
      6. {
      7. v.push_back(i);
      8. }
      9. vector<int>::iterator it = find(v.begin(), v.end(), 5);
      10. if(it == v.end())
      11. {
      12. cout << "找不到" << endl;
      13. }
      14. else
      15. {
      16. cout << "找到" << *it << endl;
      17. }
      18. }
      1. class Person
      2. {
      3. public:
      4. Person(string name, int age)
      5. {
      6. this->m_name = name;
      7. this->m_age = age;
      8. }
      9. // 重载== 让底层find知道如何对比person数据类型
      10. bool operator==(const Person &p)
      11. {
      12. if(p.m_name == this->m_name || p.m_age == this->m_age)
      13. {
      14. return true;
      15. }
      16. return false;
      17. }
      18. string m_name;
      19. int m_age;
      20. };
      21. // 查自定义数据类型
      22. void test02()
      23. {
      24. vector v;
      25. Person p1("Alice", 18);
      26. Person p2("Bob", 16);
      27. Person p3("Cycal", 20);
      28. Person p4("Davil", 19);
      29. v.push_back(p1);
      30. v.push_back(p2);
      31. v.push_back(p3);
      32. v.push_back(p4);
      33. Person pp("Bob", 16);
      34. vector::iterator it = find(v.begin(), v.end(), pp);
      35. if(it == v.end())
      36. {
      37. cout << "找不到" << endl;
      38. }
      39. else
      40. {
      41. cout << "找到" << it->m_name << " " << it->m_age << endl;
      42. }
      43. }
    • find_if:按条件查找元素

      1. class GreaterFive
      2. {
      3. public:
      4. bool operator()(int val)
      5. {
      6. return val > 5;
      7. }
      8. };
      9. // 查内置数据类型
      10. void test01()
      11. {
      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. {
      20. cout << "找不到" << endl;
      21. }
      22. else
      23. {
      24. cout << "找到" << *it << endl;
      25. }
      26. }
      1. class Person
      2. {
      3. public:
      4. Person(string name, int age)
      5. {
      6. this->m_name = name;
      7. this->m_age = age;
      8. }
      9. // 重载== 让底层find知道如何对比person数据类型
      10. bool operator==(const Person &p)
      11. {
      12. if(p.m_name == this->m_name || p.m_age == this->m_age)
      13. {
      14. return true;
      15. }
      16. return false;
      17. }
      18. string m_name;
      19. int m_age;
      20. };
      21. class Greater20
      22. {
      23. public:
      24. bool operator()(Person p)
      25. {
      26. return p.m_age > 20;
      27. }
      28. };
      29. // 查自定义数据类型
      30. void test02()
      31. {
      32. vector v;
      33. Person p1("Alice", 18);
      34. Person p2("Bob", 16);
      35. Person p3("Cycal", 22);
      36. Person p4("Davil", 19);
      37. v.push_back(p1);
      38. v.push_back(p2);
      39. v.push_back(p3);
      40. v.push_back(p4);
      41. vector::iterator it = find_if(v.begin(), v.end(), Greater20());
      42. if(it == v.end())
      43. {
      44. cout << "找不到" << endl;
      45. }
      46. else
      47. {
      48. cout << "找到" << it->m_name << " " << it->m_age << endl;
      49. }
      50. }
    • adjacent_find:查找相邻重复元素

      1. void test01()
      2. {
      3. vector<int> v;
      4. v.push_back(0);
      5. v.push_back(2);
      6. v.push_back(1);
      7. v.push_back(1);
      8. v.push_back(3);
      9. v.push_back(0);
      10. v.push_back(2);
      11. v.push_back(1);
      12. vector<int>::iterator it = adjacent_find(v.begin(), v.end());
      13. if(it == v.end())
      14. {
      15. cout << "找不到相邻重复元素" << endl;
      16. }
      17. else
      18. {
      19. cout << "找到相邻重复元素" << *it << endl;
      20. }
      21. }
    • binary_search:二分查找法(查找指定元素,查到返回true,查不到返回false)(在无序序列中不能使用)

      1. void test01()
      2. {
      3. vector<int> v;
      4. for(int i=0; i<10; i++)
      5. {
      6. v.push_back(i);
      7. }
      8. bool tag = binary_search(v.begin(), v.end(), 9);
      9. if(tag == true)
      10. {
      11. cout << "找到了" << endl;
      12. }
      13. else
      14. {
      15. cout << "找不到" << endl;
      16. }
      17. }
    • count:统计元素个数

      1. // 统计内置数据类型
      2. void test01()
      3. {
      4. vector<int> v;
      5. for(int i=0; i<10; i++)
      6. {
      7. v.push_back(i);
      8. }
      9. int num = count(v.begin(), v.end(), 2);
      10. cout << num << endl;
      11. }
      1. class Person
      2. {
      3. public:
      4. Person(string name, int age)
      5. {
      6. this->m_name = name;
      7. this->m_age = age;
      8. }
      9. // 重载== 让底层find知道如何对比person数据类型
      10. bool operator==(const Person &p)
      11. {
      12. if(p.m_age == this->m_age)
      13. {
      14. return true;
      15. }
      16. return false;
      17. }
      18. string m_name;
      19. int m_age;
      20. };
      21. // 统计自定义数据类型
      22. void test02()
      23. {
      24. vector v;
      25. Person p1("Alice", 18);
      26. Person p2("Bob", 16);
      27. Person p3("Cycal", 22);
      28. Person p4("Davil", 16);
      29. v.push_back(p1);
      30. v.push_back(p2);
      31. v.push_back(p3);
      32. v.push_back(p4);
      33. Person pp("Tim", 16);
      34. int num = count(v.begin(), v.end(), pp);
      35. cout << num << endl;
      36. }
    • count_if:按条件统计元素个数

      1. class GreaterFive
      2. {
      3. public:
      4. bool operator()(int val)
      5. {
      6. return val > 5;
      7. }
      8. };
      9. // 查内置数据类型
      10. void test01()
      11. {
      12. vector<int> v;
      13. for(int i=0; i<10; i++)
      14. {
      15. v.push_back(i);
      16. }
      17. int count = count_if(v.begin(), v.end(), GreaterFive());
      18. cout << count << endl;
      19. }
      1. class Person
      2. {
      3. public:
      4. Person(string name, int age)
      5. {
      6. this->m_name = name;
      7. this->m_age = age;
      8. }
      9. string m_name;
      10. int m_age;
      11. };
      12. class MyCompare
      13. {
      14. public:
      15. bool operator()(const Person &p)
      16. {
      17. return p.m_age > 18;
      18. }
      19. };
      20. // 查自定义数据类型
      21. void test02()
      22. {
      23. vector v;
      24. Person p1("Alice", 18);
      25. Person p2("Bob", 16);
      26. Person p3("Cycal", 20);
      27. Person p4("Davil", 19);
      28. v.push_back(p1);
      29. v.push_back(p2);
      30. v.push_back(p3);
      31. v.push_back(p4);
      32. int count = count_if(v.begin(), v.end(), MyCompare());
      33. cout << count << endl;
      34. }

  • 相关阅读:
    qt中QFileSystemModel和QStringListModel使用
    【PyTorch】深度学习实践之线性模型Linear Model
    java8新特性-stream流漫谈
    人脸自动贴国旗
    AgileConfig-1.7.0 发布,支持 SSO
    使用 Amazon Bedrock 和 Amazon SageMaker,开启全新的生成式 AI “工作年”!
    思维模型 首因效应
    WiFi网络分析工具Airtool for Mac
    【前端】JavaScript
    ATK-ESP8266使用说明(STM32-F4)
  • 原文地址:https://blog.csdn.net/weixin_57139639/article/details/138164735