• STL-函数对象、谓词、常用算法


    函数对象

    函数对象概念

    重载函数调用操作符的类,其对象常称为函数对象

    函数对象使用重载的()时,行为类似函数调用,也叫仿函数

    本质:

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

    函数对象使用

    特点:

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

    谓词

    谓词概念

    返回bool类型仿函数称为谓词

    如果operator()接受一个参数,那么叫做一元谓词

    如果operator()接受两个参数,那么叫做二元谓词

    1. //返回bool类型的仿函数称为谓词
    2. //一元谓词 - 大于5
    3. class greaterFive
    4. {
    5. public:
    6. bool operator()(int val)
    7. {
    8. return val > 5;
    9. }
    10. };
    11. void test01()
    12. {
    13. vector<int>v;
    14. for (int i = 0; i < 10; ++i)
    15. {
    16. v.push_back(i);
    17. }
    18. vector<int>::iterator it = find_if(v.begin(), v.end(), greaterFive());
    19. if (it == v.end())
    20. {
    21. cout << "没找到" << endl;
    22. }
    23. else
    24. {
    25. cout << "找到大于5的数为:" << *it << endl;
    26. }
    27. }

    二元谓词之前已经提过,排序的方式修改

    内建函数对象

    内建函数对象意义

    STL内建了一些函数对象

    分类:

    算术仿函数

    关系仿函数

    逻辑仿函数

    用法:

    这些仿函数所产生的对象,用法和一般函数完全相同

    使用内建函数对象,需要引入头文件#include

    算术仿函数

    功能描述:

    实现四则运算

    其中negate是一元运算,其他都是二元运算

    仿函数类型:

    1. void test01()
    2. {
    3. negate<int>n;
    4. cout<<n(30)<<endl;
    5. plus<int>p;
    6. cout<<p(10, 20)<<endl;
    7. }

    注意:使用内建函数对象,需要引入头文件#include

    关系仿函数

    功能描述:

    实现关系对比

    仿函数原型:

    1. void printVector(vector<int>& v)
    2. {
    3. for (vector<int>::iterator it = v.begin(); it != v.end();++it)
    4. {
    5. cout << *it << " ";
    6. }
    7. cout << endl;
    8. }
    9. class Larger
    10. {
    11. public:
    12. bool operator()(int num1, int num2)
    13. {
    14. return num1 > num2;
    15. }
    16. };
    17. void test01()
    18. {
    19. vector<int>v;
    20. v.push_back(10);
    21. v.push_back(30);
    22. v.push_back(40);
    23. v.push_back(20);
    24. printVector(v);
    25. sort(v.begin(), v.end());
    26. printVector(v);
    27. //从大到小排序
    28. // 方法1. 自定义仿函数
    29. // sort(v.begin(), v.end(), Larger());
    30. //方法2. 内建关系仿函数
    31. sort(v.begin(), v.end(), greater<int>());
    32. printVector(v);
    33. }

    关系仿函数中最常用的就是greater<>大于

    逻辑仿函数

    实现逻辑运算

    函数原型:

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

    STL-常用算法

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

    常用遍历算法

    for_each //遍历容器

    transform //搬运容器到另一个容器中

    for_each

    函数原型:

    1. //普通函数
    2. void print01(int num)
    3. {
    4. cout << num << " ";
    5. }
    6. //仿函数
    7. class print02
    8. {
    9. public:
    10. void operator()(int num)
    11. {
    12. cout << num << " ";
    13. }
    14. };
    15. void test01()
    16. {
    17. vector<int>v;
    18. v.push_back(10);
    19. v.push_back(40);
    20. v.push_back(30);
    21. v.push_back(20);
    22. //遍历算法
    23. for_each(v.begin(), v.end(), print01);
    24. cout << endl;
    25. for_each(v.begin(), v.end(), print02());
    26. }
    transform

    函数原型:

    1. class Transform
    2. {
    3. public:
    4. int operator()(int num)
    5. {
    6. return num+1;
    7. }
    8. };
    9. void test01()
    10. {
    11. vector<int>v;
    12. v.push_back(10);
    13. v.push_back(40);
    14. v.push_back(30);
    15. v.push_back(20);
    16. //搬运
    17. vector<int>v2; //目标容器
    18. v2.resize(v.size()); //目标容器,需要提前开辟空间
    19. transform(v.begin(), v.end(), v2.begin(), Transform());
    20. for_each(v2.begin(), v2.end(), print02());
    21. cout << endl;
    22. }

    常用查找算法

    算法简介

    find

    功能描述:查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()

    函数原型:

    1. void test01()
    2. {
    3. vector<int>v;
    4. v.push_back(10);
    5. v.push_back(20);
    6. v.push_back(30);
    7. v.push_back(14);
    8. for_each(v.begin(), v .end(), print02());
    9. cout << endl;
    10. vector<int>::iterator it = find(v.begin(), v.end(), 20);
    11. if (it == v.end())
    12. {
    13. cout << "没有找到" << endl;
    14. }
    15. else
    16. {
    17. cout << "找到了:" << *it << endl;
    18. }
    19. }
    20. class Person
    21. {
    22. public:
    23. Person(string name, int age)
    24. {
    25. m_Name = name;
    26. m_Age = age;
    27. }
    28. bool operator==(const Person& p)
    29. {
    30. if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
    31. {
    32. return true;
    33. }
    34. else
    35. {
    36. return false;
    37. }
    38. }
    39. string m_Name;
    40. int m_Age;
    41. };
    42. void test02()
    43. {
    44. vector<Person>v;
    45. Person p1("top", 10);
    46. Person p2("as", 32);
    47. Person p3("bob", 43);
    48. Person p4("tony", 38);
    49. v.push_back(p1);
    50. v.push_back(p2);
    51. v.push_back(p3);
    52. v.push_back(p4);
    53. Person pp("as", 32);
    54. vector<Person>::iterator it = find(v.begin(), v.end(), pp);
    55. if (it == v.end())
    56. {
    57. cout << "没找到" << endl;
    58. }
    59. else
    60. {
    61. cout << "找到了,姓名:" << it->m_Name << ",年龄:" << it->m_Age << endl;
    62. }
    63. }
    find_if

    功能描述:

    按条件查找元素

    函数原型:

    1. class greaterFive
    2. {
    3. public:
    4. bool operator()(int val)
    5. {
    6. return val > 15;
    7. }
    8. };
    9. void test01()
    10. {
    11. vector<int>v;
    12. v.push_back(10);
    13. v.push_back(20);
    14. v.push_back(30);
    15. v.push_back(14);
    16. for_each(v.begin(), v .end(), print02());
    17. cout << endl;
    18. vector<int>::iterator it = find_if(v.begin(), v.end(), greaterFive());
    19. if (it == v.end())
    20. {
    21. cout << "没有找到" << endl;
    22. }
    23. else
    24. {
    25. cout << "找到了:" << *it << endl;
    26. }
    27. }
    28. class Person
    29. {
    30. public:
    31. Person(string name, int age)
    32. {
    33. m_Name = name;
    34. m_Age = age;
    35. }
    36. bool operator==(const Person& p)
    37. {
    38. if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
    39. {
    40. return true;
    41. }
    42. else
    43. {
    44. return false;
    45. }
    46. }
    47. string m_Name;
    48. int m_Age;
    49. };
    50. class greater20
    51. {
    52. public:
    53. bool operator()(const Person& p)
    54. {
    55. return p.m_Age > 20;
    56. }
    57. };
    58. void test02()
    59. {
    60. vector<Person>v;
    61. Person p1("top", 10);
    62. Person p2("as", 32);
    63. Person p3("bob", 43);
    64. Person p4("tony", 38);
    65. v.push_back(p1);
    66. v.push_back(p2);
    67. v.push_back(p3);
    68. v.push_back(p4);
    69. Person pp("as", 32);
    70. vector<Person>::iterator it = find_if(v.begin(), v.end(), greater20());
    71. if (it == v.end())
    72. {
    73. cout << "没找到" << endl;
    74. }
    75. else
    76. {
    77. cout << "找到了,姓名:" << it->m_Name << ",年龄:" << it->m_Age << endl;
    78. }
    79. }
    adjacent_find

    查找相邻重复元素

    函数原型:

    1. void test01()
    2. {
    3. vector<int>v;
    4. v.push_back(10);
    5. v.push_back(20);
    6. v.push_back(10);
    7. v.push_back(30);
    8. v.push_back(14);
    9. v.push_back(14);
    10. vector<int>::iterator it = adjacent_find(v.begin(), v.end());
    11. if (it == v.end())
    12. {
    13. cout << "未找到" << endl;
    14. }
    15. else
    16. {
    17. cout << "已找到:" << *it << endl;
    18. }
    19. }
    binary_search

    查找指定元素是否存在

    函数原型:

    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 judge = binary_search(v.begin(), v.end(), 4);
    9. if (judge)
    10. {
    11. cout << "找到了" << endl;
    12. }
    13. else
    14. {
    15. cout << "未找到" << endl;
    16. }
    17. }

    注意:二分查找法查找效率虽然高,但查找的容器中元素必须是有序序列

    count

    统计元素个数

    函数原型:

    1. void test01()
    2. {
    3. vector<int>v;
    4. v.push_back(12);
    5. v.push_back(34);
    6. v.push_back(32);
    7. v.push_back(4);
    8. v.push_back(12);
    9. v.push_back(12);
    10. int sum = count(v.begin(), v.end(), 12);
    11. cout << "12有" << sum << "个" << endl;
    12. }
    13. class Person
    14. {
    15. public:
    16. Person(string name, int age)
    17. {
    18. m_Name = name;
    19. m_Age = age;
    20. }
    21. bool operator==(const Person& p)
    22. {
    23. if (this->m_Age == p.m_Age)
    24. {
    25. return true;
    26. }
    27. else
    28. {
    29. return false;
    30. }
    31. }
    32. string m_Name;
    33. int m_Age;
    34. };
    35. void test02()
    36. {
    37. vector<Person>v;
    38. Person p1("top", 10);
    39. Person p2("as", 32);
    40. Person p3("bob", 32);
    41. Person p4("tony", 38);
    42. v.push_back(p1);
    43. v.push_back(p2);
    44. v.push_back(p3);
    45. v.push_back(p4);
    46. Person pp("sq", 32);
    47. int sum = count(v.begin(), v.end(), pp);
    48. cout << "与sq同岁的人有" << sum << "个" << endl;
    49. }
    count_if
    1. void test01()
    2. {
    3. vector<int>v;
    4. v.push_back(12);
    5. v.push_back(34);
    6. v.push_back(32);
    7. v.push_back(4);
    8. v.push_back(16);
    9. v.push_back(12);
    10. int sum = count_if(v.begin(), v.end(), greaterFive());
    11. cout << "大于5有" << sum << "个" << endl;
    12. }
    13. class Person
    14. {
    15. public:
    16. Person(string name, int age)
    17. {
    18. m_Name = name;
    19. m_Age = age;
    20. }
    21. string m_Name;
    22. int m_Age;
    23. };
    24. class AgeGreater20
    25. {
    26. public:
    27. bool operator()(const Person&p)
    28. {
    29. return p.m_Age > 20;
    30. }
    31. };
    32. void test02()
    33. {
    34. vector<Person>v;
    35. Person p1("top", 10);
    36. Person p2("as", 32);
    37. Person p3("bob", 32);
    38. Person p4("tony", 38);
    39. Person p5("pig", 43);
    40. v.push_back(p1);
    41. v.push_back(p2);
    42. v.push_back(p3);
    43. v.push_back(p4);
    44. v.push_back(p5);
    45. Person pp("sq", 32);
    46. int sum = count_if(v.begin(), v.end(), AgeGreater20());
    47. cout << "大于20的人有" << sum << "个" << endl;
    48. }

    常用排序算法

    sort

    函数原型:

    1. class print02
    2. {
    3. public:
    4. void operator()(int num)
    5. {
    6. cout << num << " ";
    7. }
    8. };
    9. class greaterInt
    10. {
    11. public:
    12. bool operator()(int a, int b)
    13. {
    14. return a > b;
    15. }
    16. };
    17. void test01()
    18. {
    19. vector<int> v;
    20. v.push_back(10);
    21. v.push_back(30);
    22. v.push_back(40);
    23. v.push_back(20);
    24. v.push_back(50);
    25. sort(v.begin(), v.end());//升序
    26. for_each(v.begin(), v.end(), print02());
    27. cout << endl;
    28. //降序
    29. //sort(v.begin(), v.end(), greater<int>());
    30. sort(v.begin(), v.end(), greaterInt());
    31. for_each(v.begin(), v.end(), print02());
    32. cout << endl;
    33. }
    random_shuffle

    洗牌,指定范围内的元素随机调整次序

    函数原型:

    1. void test01()
    2. {
    3. srand((unsigned int)time(NULL));
    4. vector<int> v;
    5. for (int i = 0; i < 10; ++i)
    6. {
    7. v.push_back(i);
    8. }
    9. for_each(v.begin(), v.end(), print02());
    10. cout << endl;
    11. //利用洗牌算法 打乱顺序
    12. random_shuffle(v.begin(), v.end());
    13. for_each(v.begin(), v.end(), print02());
    14. cout << endl;
    15. }

    random_shuffle洗牌算法较实用,记得加随机种子

    merge

    两个有序的容器元素合并,并存储到另一容器中

    合并之后的容器依旧是有序的

    函数原型:

    reverse

    反转元素

    函数原型:

    1. void test01()
    2. {
    3. srand((unsigned int)time(NULL));
    4. vector<int> v;
    5. for (int i = 0; i < 10; ++i)
    6. {
    7. v.push_back(i);
    8. }
    9. for_each(v.begin(), v.end(), print02());
    10. cout << endl;
    11. reverse(v.begin(), v.end());
    12. for_each(v.begin(), v.end(), print02());
    13. cout << endl;
    14. }

    常用拷贝和替换算法

    copy

    copy(v.begin(), v.end(), v2.begin());

    1. void test01()
    2. {
    3. srand((unsigned int)time(NULL));
    4. vector<int> v;
    5. vector<int> v2;
    6. for (int i = 0; i < 10; ++i)
    7. {
    8. v.push_back(i);
    9. }
    10. for_each(v.begin(), v.end(), print02());
    11. cout << endl;
    12. v2.resize(v.size());
    13. copy(v.begin(), v.end(), v2.begin());
    14. for_each(v2.begin(), v2.end(), print02());
    15. cout << endl;
    16. }
    replace

    1. void test01()
    2. {
    3. srand((unsigned int)time(NULL));
    4. vector<int> v;
    5. for (int i = 0; i < 10; ++i)
    6. {
    7. v.push_back(i);
    8. }
    9. v.push_back(2);
    10. v.push_back(2);
    11. for_each(v.begin(), v.end(), print02());
    12. cout << endl;
    13. replace(v.begin(), v.end(), 2, 200);//将所有2改为200
    14. for_each(v.begin(), v.end(), print02());
    15. cout << endl;
    16. }
    replace_if

    函数原型:

    1. void test01()
    2. {
    3. srand((unsigned int)time(NULL));
    4. vector<int> v;
    5. for (int i = 0; i < 10; ++i)
    6. {
    7. v.push_back(i);
    8. }
    9. v.push_back(2);
    10. v.push_back(2);
    11. for_each(v.begin(), v.end(), print02());
    12. cout << endl;
    13. replace_if(v.begin(), v.end(), greaterFive(), 6);//将所有大于5的改为6
    14. for_each(v.begin(), v.end(), print02());
    15. cout << endl;
    16. }
    swap

    函数原型:

    常用算术生成算法

    算法生成算法属于小型算法,使用时包含头文件#include

    accumulate

    计算区间内 容器元素累计总和

    函数原型:

    1. #include<numeric>
    2. void test01()
    3. {
    4. vector<int> v;
    5. for (int i = 0; i <= 100; ++i)
    6. {
    7. v.push_back(i);
    8. }
    9. //参数3是其实累加值
    10. int total = accumulate(v.begin(), v.end(), 0); //5050
    11. total = accumulate(v.begin(), v.end(), 1000);//6050
    12. //for_each(v.begin(), v.end(), print02());
    13. cout << total<< endl;
    14. }
    fill

    向容器中填充指定的元素

    函数原型:

    1. void test01()
    2. {
    3. vector<int>v;
    4. v.resize(10);//初始化100
    5. fill(v.begin(), v.end(), 100);//0重新填充为100
    6. }

    常用集合算法

    set_intersection

    交集:

    例:

    v1: 0 1 2 3 4 5 6 7 8 9 10

    v2: 5 6 7 8 9 10 11 12 13 14 15

    交集: 5 6 7 8 9 10

    两个集合必须是有序序列!!

    1. void test01()
    2. {
    3. vector<int> v;
    4. vector<int> v2;
    5. for (int i = 0; i <= 10; ++i)
    6. {
    7. v.push_back(i);
    8. v2.push_back(i+5);
    9. }
    10. vector<int>vTarget;
    11. //目标容器需要提前开辟空间
    12. //最特殊情况也是占用空间最大情况:大容器包含小容器,
    13. //故开辟空间时取最小容器的size
    14. vTarget.resize(min(v.size(), v2.size()));
    15. //获取交集
    16. //返回目标容器的最后一个元素的迭代器地址
    17. vector<int>::iterator itEnd = set_intersection(v.begin(), v.end(), v2.begin(), v2.end(), vTarget.begin());
    18. for_each(vTarget.begin(), itEnd, print02());
    19. cout << endl;
    20. }
    set_union

    求集合并集

    例:

    v1: 0 1 2 3 4 5 6 7 8 9 10

    v2: 5 6 7 8 9 10 11 12 13 14 15

    并集: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    1. void test01()
    2. {
    3. vector<int> v;
    4. vector<int> v2;
    5. for (int i = 0; i <= 10; ++i)
    6. {
    7. v.push_back(i);
    8. v2.push_back(i+5);
    9. }
    10. for_each(v.begin(), v.end(), print02());
    11. cout << endl;
    12. for_each(v2.begin(), v2.end(), print02());
    13. cout << endl;
    14. vector<int>vTarget;
    15. //目标容器需要提前开辟空间
    16. //最特殊情况也是占用空间最大情况:两个容器没有交集,
    17. //并集就是两个容器size相加
    18. vTarget.resize(v.size()+v2.size());
    19. //获取并集
    20. vector<int>::iterator itEnd = set_union(v.begin(), v.end(), v2.begin(), v2.end(), vTarget.begin());
    21. for_each(vTarget.begin(), itEnd, print02());
    22. cout << endl;
    23. }
    set_difference

    差集

    例:

    v1: 0 1 2 3 4 5 6 7 8 9 10

    v2: 5 6 7 8 9 10 11 12 13 14 15

    v1和v2容器的差集: 0 1 2 3 4

    v2和v1容器的差集:11 12 13 14 15

    1. void test01()
    2. {
    3. vector<int> v;
    4. vector<int> v2;
    5. for (int i = 0; i <= 10; ++i)
    6. {
    7. v.push_back(i);
    8. v2.push_back(i+5);
    9. }
    10. for_each(v.begin(), v.end(), print02());
    11. cout << endl;
    12. for_each(v2.begin(), v2.end(), print02());
    13. cout << endl;
    14. vector<int>vTarget;
    15. //目标容器需要提前开辟空间
    16. //最特殊情况也是占用空间最大情况:两个容器没有交集,
    17. //差集就是两个容器中大的size作为目标容器开辟空间
    18. vTarget.resize(max(v.size(), v2.size()));
    19. //获取v1和v2的差集
    20. cout << "v1和v2的差集为:" << endl;
    21. vector<int>::iterator itEnd = set_difference(v.begin(), v.end(), v2.begin(), v2.end(), vTarget.begin());
    22. for_each(vTarget.begin(), itEnd, print02());
    23. cout << endl;
    24. cout << "v2和v1的差集为:" << endl;
    25. itEnd = set_difference(v2.begin(), v2.end(), v.begin(), v.end(), vTarget.begin());
    26. for_each(vTarget.begin(), itEnd, print02());
    27. cout << endl;
    28. }
  • 相关阅读:
    巧用 CSS 构建渐变彩色二维码
    Java手写链表和案例拓展
    计算机图形学之点和直线
    第七届中美创新链接峰会-区块链与绿色发展
    C语言之位域(位段)入门详解
    SSM考试题库管理系统毕业设计源码069043
    JUC包-线程池
    百万奖池角逐,华为云IoT边缘带你看懂“边缘计算开发者大赛”
    LeetCode刷题日记003——无重复最长子串
    如何用 GPTs 构建自己的知识分身?(进阶篇)
  • 原文地址:https://blog.csdn.net/ThePaK/article/details/133223501