• C++阶段05笔记06【C++提高编程资料(常用遍历算法、常用查找算法、常用排序算法、常用拷贝和替换算法、常用算术生成算法、常用集合算法)】


    C++匠心之作-从0到1入门学编程【视频+课件+笔记+源码】

    目录

    5、STL常用算法

    5.1、常用遍历算法

    5.1.1、for_each

    5.1.2、transform

    5.2、常用查找算法

    5.2.1、find

    5.2.2、find_if

    5.2.3、adjacent_find

    5.2.4、binary_search

    5.2.5、count

    5.2.6、count_if

    5.3、常用排序算法

    5.3.1、sort

    5.3.2、random_shuffle

    5.3.3、merge

    5.3.4、reverse

    5.4、常用拷贝和替换算法

    5.4.1、copy

    5.4.2、replace

    5.4.3、replace_if

    5.4.4、swap

    5.5、常用算术生成算法

    5.5.1、accumulate

    5.5.2、fill

    5.6、常用集合算法

    5.6.1、set_intersection

    5.6.2、set_union

    5.6.3、set_difference


    5、STL常用算法

    概述:算法主要是由头文件组成。

    1. 是所有STL头文件中最大的一个,范围涉及到比较、 交换、查找、遍历操作、复制、修改等等;

    2. 体积很小,只包括几个在序列上面进行简单数学运算的模板函数;

    3. 定义了一些模板类用以声明函数对象。

    5.1、常用遍历算法

    学习目标:掌握常用的遍历算法.

    算法简介:

    • for_each //遍历容器

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

    5.1.1、for_each

    功能描述:实现遍历容器。

    函数原型:

    • for_each(iterator beg, iterator end, _func);

      //遍历算法:遍历容器元素

      //beg:开始迭代器

      //end:结束迭代器

      //_func:函数或者函数对象

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用遍历算法-for_each
    6. //普通函数
    7. void print01(int val) {
    8. cout << val << " ";
    9. }
    10. //仿函数-函数对象
    11. class print02 {
    12. public:
    13. void operator()(int val) {
    14. cout << val << " ";
    15. }
    16. };
    17. //for_each算法基本用法
    18. void test01() {
    19. vector<int> v;
    20. for (int i = 0; i < 10; i++) {
    21. v.push_back(i);
    22. }
    23. //遍历算法
    24. for_each(v.begin(), v.end(), print01);
    25. cout << endl;
    26. for_each(v.begin(), v.end(), print02());
    27. cout << endl;
    28. }
    29. int main() {
    30. test01();
    31. system("pause");
    32. return 0;
    33. }

    总结:for_each在实际开发中是最常用遍历算法,需要熟练掌握。

    5.1.2、transform

    功能描述:搬运容器到另一个容器中。

    函数原型:

    • transform(iterator beg1, iterator end1, iterator beg2, _func);

            //beg1:源容器开始迭代器

            //end1:源容器结束迭代器

            //beg2:目标容器开始迭代器

            //_func:函数或者函数对象

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用遍历算法-transform搬运
    6. class Transform {
    7. public:
    8. int operator()(int val) {
    9. return val + 100;
    10. }
    11. };
    12. class MyPrint {
    13. public:
    14. void operator()(int val) {
    15. cout << val << " ";
    16. }
    17. };
    18. void test01() {
    19. vector<int> v;
    20. for (int i = 0; i < 10; i++) {
    21. v.push_back(i);
    22. }
    23. vector<int> vTarget;//目标容器
    24. vTarget.resize(v.size());//目标容器需要提前开辟空间
    25. transform(v.begin(), v.end(), vTarget.begin(), Transform());
    26. for_each(vTarget.begin(), vTarget.end(), MyPrint());
    27. cout << endl;
    28. }
    29. int main() {
    30. test01();
    31. system("pause");
    32. return 0;
    33. }

    总结: 搬运的目标容器必须要提前开辟空间,否则无法正常搬运。

    5.2、常用查找算法

    学习目标:掌握常用的查找算法。

    算法简介:

    1. find //查找元素

    2. find_if //按条件查找元素

    3. adjacent_find //查找相邻重复元素

    4. binary_search //二分查找法

    5. count //统计元素个数

    6. count_if //按条件统计元素个数

    5.2.1、find

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

    函数原型:

    • find(iterator beg, iterator end, value);

      //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

      //beg:开始迭代器;end:结束迭代器;value:查找的元素

    示例:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. //常用查找算法-find
    7. //查找-内置数据类型
    8. void test01() {
    9. vector<int> v;
    10. for (int i = 0; i < 10; i++) {
    11. v.push_back(i);//(i + 1);
    12. }
    13. //查找容器中是否有“5”这个元素
    14. vector<int>::iterator it = find(v.begin(), v.end(), 5);
    15. if (it == v.end()) {
    16. cout << "没有找到!" << endl;
    17. } else {
    18. cout << "找到:" << *it << endl;
    19. }
    20. }
    21. class Person {
    22. public:
    23. Person(string name, int age) {
    24. this->m_Name = name;
    25. this->m_Age = age;
    26. }
    27. //重载==,底层find知道如何对比person数据类型
    28. bool operator==(const Person &p) {
    29. if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
    30. return true;
    31. } else {
    32. return false;
    33. }
    34. //return false;
    35. }
    36. public:
    37. string m_Name;
    38. int m_Age;
    39. };
    40. //查找自定义数据类型
    41. void test02() {
    42. vector v;
    43. //创建数据
    44. Person p1("aaa", 10);
    45. Person p2("bbb", 20);
    46. Person p3("ccc", 30);
    47. Person p4("ddd", 40);
    48. //放入到容器中
    49. v.push_back(p1);
    50. v.push_back(p2);
    51. v.push_back(p3);
    52. v.push_back(p4);
    53. Person pp("bbb", 20);
    54. vector::iterator it = find(v.begin(), v.end(), pp);
    55. if (it == v.end()) {
    56. cout << "没有找到!" << endl;
    57. } else {
    58. cout << "找到元素,姓名:" << it->m_Name << "、年龄:" << it->m_Age << endl;
    59. }
    60. }
    61. int main() {
    62. test01();
    63. test02();
    64. system("pause");
    65. return 0;
    66. }

    总结:利用find可以在容器中找指定的元素,返回值是迭代器

    5.2.2、find_if

    功能描述:按条件查找元素。

    函数原型:

    • find_if(iterator beg, iterator end, _Pred);

      //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

      //beg:开始迭代器;end:结束迭代器;_Pred:函数或者谓词(返回bool类型的仿函数)

    示例:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. //常用查找算法-find_if
    7. //1、查找内置数据类型
    8. class GreaterFive {
    9. public:
    10. bool operator()(int val) {
    11. return val > 5;
    12. }
    13. };
    14. void test01() {
    15. vector<int> v;
    16. for (int i = 0; i < 10; i++) {
    17. v.push_back(i);//(i + 1);
    18. }
    19. vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
    20. if (it == v.end()) {
    21. cout << "没有找到!" << endl;
    22. } else {
    23. cout << "找到大于5的数字为:" << *it << endl;
    24. }
    25. }
    26. //2、查找自定义数据类型
    27. class Person {
    28. public:
    29. Person(string name, int age) {
    30. this->m_Name = name;
    31. this->m_Age = age;
    32. }
    33. public:
    34. string m_Name;
    35. int m_Age;
    36. };
    37. class Greater20 {
    38. public:
    39. bool operator()(Person &p) {
    40. return p.m_Age > 20;
    41. }
    42. };
    43. void test02() {
    44. vector v;
    45. //创建数据
    46. Person p1("aaa", 10);
    47. Person p2("bbb", 20);
    48. Person p3("ccc", 30);
    49. Person p4("ddd", 40);
    50. v.push_back(p1);
    51. v.push_back(p2);
    52. v.push_back(p3);
    53. v.push_back(p4);
    54. //找年龄大于20的人
    55. vector::iterator it = find_if(v.begin(), v.end(), Greater20());
    56. if (it == v.end()) {
    57. cout << "没有找到!" << endl;
    58. } else {
    59. cout << "找到,姓名:" << it->m_Name << "、年龄:" << it->m_Age << endl;
    60. }
    61. }
    62. int main() {
    63. test01();
    64. test02();
    65. system("pause");
    66. return 0;
    67. }

    总结:find_if按条件查找使查找更加灵活,提供的仿函数可以改变不同的策略。

    5.2.3、adjacent_find

    功能描述:查找相邻重复元素。

    函数原型:

    • adjacent_find(iterator beg, iterator end);

      //查找相邻重复元素,返回相邻元素的第一个位置的迭代器

      //beg:开始迭代器;end:结束迭代器

    示例:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. //常用查找算法-find_if
    7. //1、查找内置数据类型
    8. class GreaterFive {
    9. public:
    10. bool operator()(int val) {
    11. return val > 5;
    12. }
    13. };
    14. void test01() {
    15. vector<int> v;
    16. for (int i = 0; i < 10; i++) {
    17. v.push_back(i);//(i + 1);
    18. }
    19. vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
    20. if (it == v.end()) {
    21. cout << "没有找到!" << endl;
    22. } else {
    23. cout << "找到大于5的数字为:" << *it << endl;
    24. }
    25. }
    26. //2、查找自定义数据类型
    27. class Person {
    28. public:
    29. Person(string name, int age) {
    30. this->m_Name = name;
    31. this->m_Age = age;
    32. }
    33. public:
    34. string m_Name;
    35. int m_Age;
    36. };
    37. class Greater20 {
    38. public:
    39. bool operator()(Person &p) {
    40. return p.m_Age > 20;
    41. }
    42. };
    43. void test02() {
    44. vector v;
    45. //创建数据
    46. Person p1("aaa", 10);
    47. Person p2("bbb", 20);
    48. Person p3("ccc", 30);
    49. Person p4("ddd", 40);
    50. v.push_back(p1);
    51. v.push_back(p2);
    52. v.push_back(p3);
    53. v.push_back(p4);
    54. //找年龄大于20的人
    55. vector::iterator it = find_if(v.begin(), v.end(), Greater20());
    56. if (it == v.end()) {
    57. cout << "没有找到!" << endl;
    58. } else {
    59. cout << "找到,姓名:" << it->m_Name << "、年龄:" << it->m_Age << endl;
    60. }
    61. }
    62. int main() {
    63. test01();
    64. test02();
    65. system("pause");
    66. return 0;
    67. }

    总结:面试题中如果出现查找相邻重复元素,记得用STL中的adjacent_find算法。

    功能描述:查找指定元素是否存在。

    函数原型:

    • bool binary_search(iterator beg, iterator end, value);

      //查找指定的元素,查到返回true、否则false

      //注意:在无序序列中不可用

      //beg:开始迭代器;end:结束迭代器;value:查找的元素

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用查找算法-binary_search
    6. void test01() {
    7. vector<int> v;
    8. for (int i = 0; i < 10; i++) {
    9. v.push_back(i);
    10. }
    11. //v.push_back(2);如果是无序序列,结果未知!
    12. //查找容器中是否有9元素
    13. //注意:容器必须是有序的序列
    14. bool ret = binary_search(v.begin(), v.end(), 9);
    15. // bool ret = binary_search(v.begin(), v.end(),2);//二分查找
    16. if (ret) {
    17. cout << "找到了元素!" << endl;
    18. } else {
    19. cout << "未找到!" << endl;
    20. }
    21. }
    22. int main() {
    23. test01();
    24. system("pause");
    25. return 0;
    26. }

    总结:二分查找法查找效率很高,值得注意的是查找的容器中元素必须的有序序列。

    5.2.5、count

    功能描述:统计元素个数。

    函数原型:

    • count(iterator beg, iterator end, value);

      // 统计元素出现次数

      //beg:开始迭代器;end:结束迭代器;value:统计的元素

    示例:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. //常用查找算法-count
    7. //1、统计内置数据类型
    8. void test01() {
    9. vector<int> v;
    10. v.push_back(10);
    11. v.push_back(40);
    12. v.push_back(30);
    13. v.push_back(40);
    14. v.push_back(20);
    15. v.push_back(40);
    16. int num = count(v.begin(), v.end(), 40);
    17. cout << "40的元素个数为: " << num << endl;//3
    18. }
    19. //2、统计自定义数据类型
    20. class Person {
    21. public:
    22. Person(string name, int age) {
    23. this->m_Name = name;
    24. this->m_Age = age;
    25. }
    26. bool operator==(const Person &p) {
    27. if (this->m_Age == p.m_Age) {
    28. return true;
    29. } else {
    30. return false;
    31. }
    32. }
    33. string m_Name;
    34. int m_Age;
    35. };
    36. void test02() {
    37. vector v;
    38. Person p1("刘备", 35);
    39. Person p2("关羽", 35);
    40. Person p3("张飞", 35);
    41. Person p4("赵云", 30);
    42. Person p5("曹操", 40);
    43. //将人员插入到容器中
    44. v.push_back(p1);
    45. v.push_back(p2);
    46. v.push_back(p3);
    47. v.push_back(p4);
    48. v.push_back(p5);
    49. Person p("诸葛亮", 35);
    50. int num = count(v.begin(), v.end(), p);
    51. cout << "和诸葛亮同岁数的人员个数num = :" << num << endl;//3
    52. }
    53. int main() {
    54. test01();
    55. test02();
    56. system("pause");
    57. return 0;
    58. }

    总结: 统计自定义数据类型时候,需要配合重载operator==

    5.2.6、count_if

    功能描述:按条件统计元素个数。

    函数原型:

    • count_if(iterator beg, iterator end, _Pred);

      //按条件统计元素出现次数

      //beg:开始迭代器;end:结束迭代器;_Pred:谓词

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用查找算法-count_if
    6. //统计内置数据类型
    7. class Greater20 {
    8. public:
    9. bool operator()(int val) {
    10. return val > 20;
    11. }
    12. };
    13. //内置数据类型
    14. void test01() {
    15. vector<int> v;
    16. v.push_back(10);
    17. v.push_back(40);
    18. v.push_back(30);
    19. v.push_back(20);
    20. v.push_back(40);
    21. v.push_back(20);
    22. int num = count_if(v.begin(), v.end(), Greater20());
    23. cout << "大于20的元素个数为:" << num << endl;
    24. }
    25. //自定义数据类型
    26. class Person {
    27. public:
    28. Person(string name, int age) {
    29. this->m_Name = name;
    30. this->m_Age = age;
    31. }
    32. string m_Name;
    33. int m_Age;
    34. };
    35. class AgeGreater20 {
    36. public:
    37. bool operator()(const Person &p) {//谓词
    38. return p.m_Age > 20;
    39. }
    40. };
    41. //统计自定义数据类型
    42. void test02() {
    43. vector v;
    44. Person p1("刘备", 35);
    45. Person p2("关羽", 35);
    46. Person p3("张飞", 35);
    47. Person p4("赵云", 40);
    48. Person p5("曹操", 20);
    49. v.push_back(p1);
    50. v.push_back(p2);
    51. v.push_back(p3);
    52. v.push_back(p4);
    53. v.push_back(p5);
    54. //统计大于20岁人员个数
    55. int num = count_if(v.begin(), v.end(), AgeGreater20());
    56. cout << "大于20岁的人员个数为:" << num << endl;
    57. }
    58. int main() {
    59. test01();
    60. test02();
    61. system("pause");
    62. return 0;
    63. }

    总结:按值统计用count,按条件统计用count_if。

    5.3、常用排序算法

    学习目标:掌握常用的排序算法。

    算法简介:

    1. sort //对容器内元素进行排序

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

    3. merge //容器元素合并,并存储到另一容器中

    4. reverse //反转指定范围的元素

    5.3.1、sort

    功能描述:对容器内元素进行排序。

    函数原型:

    • sort(iterator beg, iterator end, _Pred);

      //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

      //beg:开始迭代器;end:结束迭代器;_Pred:谓词

    示例:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. //常用排序算法-sort
    7. void myPrint(int val) {
    8. cout << val << " ";
    9. }
    10. void test01() {
    11. vector<int> v;
    12. v.push_back(10);
    13. v.push_back(30);
    14. v.push_back(50);
    15. v.push_back(20);
    16. v.push_back(40);
    17. //利用sort进行升序,sort默认从小到大排序
    18. sort(v.begin(), v.end());
    19. for_each(v.begin(), v.end(), myPrint);
    20. cout << endl;
    21. //改变为降序,从大到小排序
    22. sort(v.begin(), v.end(), greater<int>());
    23. for_each(v.begin(), v.end(), myPrint);
    24. cout << endl;
    25. }
    26. int main() {
    27. test01();
    28. system("pause");
    29. return 0;
    30. }

    总结:sort属于开发中最常用的算法之一,需熟练掌握。

    5.3.2、random_shuffle

    功能描述:洗牌,指定范围内的元素随机调整次序。

    函数原型:

    • random_shuffle(iterator beg, iterator end);

      //指定范围内的元素随机调整次序

      //beg:开始迭代器;end:结束迭代器

    示例:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. //常用排序算法-random_shuffle
    7. class myPrint {
    8. public:
    9. void operator()(int val) {
    10. cout << val << " ";
    11. }
    12. };
    13. void myPrint(int val) {
    14. cout << val << " ";
    15. }
    16. void test01() {
    17. srand((unsigned int)time(NULL));
    18. vector<int> v;
    19. for (int i = 0; i < 10; i++) {
    20. v.push_back(i);
    21. }
    22. for_each(v.begin(), v.end(), myPrint);
    23. cout << endl;
    24. //利用洗牌算法打乱顺序
    25. random_shuffle(v.begin(), v.end());
    26. for_each(v.begin(), v.end(), myPrint);
    27. cout << endl;
    28. }
    29. int main() {
    30. test01();
    31. system("pause");
    32. return 0;
    33. }

    总结:random_shuffle洗牌算法比较实用,使用时记得加随机数种子。

    5.3.3、merge

    功能描述:两个容器元素合并,并存储到另一容器中。

    函数原型:

    • merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

      //容器元素合并,并存储到另一容器中

      //注意:两个容器必须是有序的

      //beg1:容器1开始迭代器;end1:容器1结束迭代器

      //beg2:容器2开始迭代器;end2:容器2结束迭代器

      //dest:目标容器开始迭代器

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用排序算法-merge
    6. void myPrint(int val) {
    7. cout << val << " ";
    8. }
    9. class myPrint {
    10. public:
    11. void operator()(int val) {
    12. cout << val << " ";
    13. }
    14. };
    15. //常用排序算法-merge
    16. void test01() {
    17. vector<int> v1;
    18. vector<int> v2;
    19. for (int i = 0; i < 10; i++) {
    20. v1.push_back(i);
    21. v2.push_back(i + 1);
    22. }
    23. vector<int> vTarget;//目标容器
    24. //提前给目标容器分配空间,目标容器需要提前开辟空间
    25. vTarget.resize(v1.size() + v2.size());
    26. //合并,需要两个有序序列
    27. merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    28. for_each(vTarget.begin(), vTarget.end(), myPrint);
    29. cout << endl;
    30. }
    31. int main() {
    32. test01();
    33. system("pause");
    34. return 0;
    35. }

    总结:merge合并的两个容器必须的有序序列,合并后的序列仍然有序。

    5.3.4、reverse

    功能描述:将容器内元素进行反转。

    函数原型:

    • reverse(iterator beg, iterator end);

      //反转指定范围的元素

      //beg:开始迭代器;end:结束迭代器

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用排序算法-reverse
    6. void myPrint(int val) {
    7. cout << val << " ";
    8. }
    9. class myPrint {
    10. public:
    11. void operator()(int val) {
    12. cout << val << " ";
    13. }
    14. };
    15. void test01() {
    16. vector<int> v;
    17. v.push_back(10);
    18. v.push_back(30);
    19. v.push_back(50);
    20. v.push_back(20);
    21. v.push_back(40);
    22. cout << "反转前:" << endl;
    23. for_each(v.begin(), v.end(), myPrint);
    24. cout << endl;
    25. cout << "反转后:" << endl;
    26. reverse(v.begin(), v.end());
    27. for_each(v.begin(), v.end(), myPrint);
    28. cout << endl;
    29. }
    30. int main() {
    31. test01();
    32. system("pause");
    33. return 0;
    34. }

    总结:reverse反转区间内元素,面试题可能涉及到。

    5.4、常用拷贝和替换算法

    学习目标:掌握常用的拷贝和替换算法。

    算法简介:

    1. copy // 容器内指定范围的元素拷贝到另一容器中

    2. replace // 将容器内指定范围的旧元素修改为新元素

    3. replace_if // 容器内指定范围满足条件的元素替换为新元素

    4. swap // 互换两个容器的元素

    5.4.1、copy

    功能描述:容器内指定范围的元素拷贝到另一容器中。

    函数原型:

    • copy(iterator beg, iterator end, iterator dest);

      //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

      //beg:开始迭代器;end:结束迭代器;dest:目标起始迭代器

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用拷贝和替换算法-copy
    6. void myPrint(int val) {
    7. cout << val << " ";
    8. }
    9. class myPrint {
    10. public:
    11. void operator()(int val) {
    12. cout << val << " ";
    13. }
    14. };
    15. void test01() {
    16. vector<int> v1;
    17. for (int i = 0; i < 10; i++) {
    18. v1.push_back(i);
    19. }
    20. vector<int> v2;
    21. v2.resize(v1.size());
    22. copy(v1.begin(), v1.end(), v2.begin());
    23. for_each(v2.begin(), v2.end(), myPrint);
    24. cout << endl;
    25. }
    26. int main() {
    27. test01();
    28. system("pause");
    29. return 0;
    30. }

    总结:利用copy算法在拷贝时,目标容器记得提前开辟空间。

    5.4.2、replace

    功能描述:将容器内指定范围的旧元素修改为新元素。

    函数原型:

    • replace(iterator beg, iterator end, oldvalue, newvalue);

      //将区间内旧元素替换成新元素

      //beg:开始迭代器;end:结束迭代器;oldvalue:旧元素;newvalue:新元素

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用拷贝和替换算法-replace
    6. class MyPrint {
    7. public:
    8. void operator()(int val) {
    9. cout << val << " ";
    10. }
    11. };
    12. void test01() {
    13. vector<int> v;
    14. v.push_back(20);
    15. v.push_back(30);
    16. v.push_back(50);
    17. v.push_back(30);
    18. v.push_back(40);
    19. v.push_back(20);
    20. v.push_back(10);
    21. v.push_back(20);
    22. cout << "替换前:" << endl;
    23. for_each(v.begin(), v.end(), MyPrint());
    24. cout << endl;
    25. //将容器中的20替换成2000
    26. cout << "替换后:" << endl;
    27. replace(v.begin(), v.end(), 20, 2000);
    28. for_each(v.begin(), v.end(), MyPrint());
    29. cout << endl;
    30. }
    31. int main() {
    32. test01();
    33. system("pause");
    34. return 0;
    35. }

    总结:replace会替换区间内满足条件的元素。

    5.4.3、replace_if

    功能描述:将区间内满足条件的元素,替换成指定元素。

    函数原型:

    • replace_if(iterator beg, iterator end, _pred, newvalue);

      //按条件替换元素,满足条件的替换成指定元素

      //beg:开始迭代器;end:结束迭代器;_pred:谓词;newvalue:替换的新元素

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用拷贝和替换算法-replace_if
    6. class MyPrint {
    7. public:
    8. void operator()(int val) {
    9. cout << val << " ";
    10. }
    11. };
    12. class Greater30 {//ReplaceGreater30
    13. public:
    14. bool operator()(int val) {
    15. return val >= 30;
    16. }
    17. };
    18. void test01() {
    19. vector<int> v;
    20. v.push_back(10);
    21. v.push_back(40);
    22. v.push_back(20);
    23. v.push_back(40);
    24. v.push_back(30);
    25. v.push_back(50);
    26. v.push_back(20);
    27. v.push_back(30);
    28. cout << "替换前:" << endl;
    29. for_each(v.begin(), v.end(), MyPrint());
    30. cout << endl;
    31. //将大于等于30的元素替换为3000,将容器中大于等于30的元素替换成3000
    32. cout << "替换后:" << endl;
    33. replace_if(v.begin(), v.end(), Greater30(), 3000);
    34. for_each(v.begin(), v.end(), MyPrint());
    35. cout << endl;
    36. }
    37. int main() {
    38. test01();
    39. system("pause");
    40. return 0;
    41. }

    总结:replace_if按条件查找,可以利用仿函数灵活筛选满足的条件。

    5.4.4、swap

    功能描述:互换两个容器的元素。

    函数原型:

    • swap(container c1, container c2);

      //互换两个容器的元素

      //c1:容器1;c2:容器2

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用拷贝和替换算法-swap
    6. void myPrint(int val) {
    7. cout << val << " ";
    8. }
    9. class myPrint {
    10. public:
    11. void operator()(int val) {
    12. cout << val << " ";
    13. }
    14. };
    15. void test01() {
    16. vector<int> v1;
    17. vector<int> v2;
    18. for (int i = 0; i < 10; i++) {
    19. v1.push_back(i);
    20. v2.push_back(i + 100);
    21. }
    22. cout << "交换前: " << endl;
    23. for_each(v1.begin(), v1.end(), myPrint);
    24. cout << endl;
    25. for_each(v2.begin(), v2.end(), myPrint);
    26. cout << endl;
    27. cout << "-----------------" << endl;
    28. cout << "交换后: " << endl;
    29. swap(v1, v2);
    30. for_each(v1.begin(), v1.end(), myPrint);
    31. cout << endl;
    32. for_each(v2.begin(), v2.end(), myPrint);
    33. cout << endl;
    34. }
    35. int main() {
    36. test01();
    37. system("pause");
    38. return 0;
    39. }

    总结:swap交换容器时,注意交换的容器要同种类型。

    5.5、常用算术生成算法

    学习目标:掌握常用的算术生成算法。

    注意:算术生成算法属于小型算法,使用时包含的头文件为#include

    算法简介:

    • accumulate //计算容器元素累计总和

    • fill //向容器中添加元素

    5.5.1、accumulate

    功能描述:计算区间内容器元素累计总和。

    函数原型:

    • accumulate(iterator beg, iterator end, value);

      //计算容器元素累计总和

      //beg:开始迭代器;end:结束迭代器;value:起始值

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用算术生成算法-accumulate
    6. void test01() {
    7. vector<int> v;
    8. for (int i = 0; i <= 100; i++) {
    9. v.push_back(i);
    10. }
    11. //参数3:起始累加值
    12. int total = accumulate(v.begin(), v.end(), 0);
    13. cout << "total = " << total << endl;//5050
    14. }
    15. int main() {
    16. test01();
    17. system("pause");
    18. return 0;
    19. }

    总结:accumulate使用时头文件注意是 numeric,这个算法很实用。

    5.5.2、fill

    功能描述:向容器中填充指定的元素。

    函数原型:

    • fill(iterator beg, iterator end, value);

      //向容器中填充元素

      //beg:开始迭代器;end:结束迭代器;value:填充的值

    示例:

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. //常用算术生成算法-fill
    7. void myPrint(int val) {
    8. cout << val << " ";
    9. }
    10. class myPrint {
    11. public:
    12. void operator()(int val) {
    13. cout << val << " ";
    14. }
    15. };
    16. void test01() {
    17. vector<int> v;
    18. v.resize(10);
    19. //后期重新填充
    20. fill(v.begin(), v.end(), 100);
    21. for_each(v.begin(), v.end(), myPrint);
    22. cout << endl;
    23. }
    24. int main() {
    25. test01();
    26. system("pause");
    27. return 0;
    28. }

    总结:利用fill可以将容器区间内元素填充为指定的值

    5.6、常用集合算法

    学习目标:掌握常用的集合算法。

    算法简介:

    • set_intersection // 求两个容器的交集

    • set_union // 求两个容器的并集

    • set_difference // 求两个容器的差集

    常用集合算法解析图

    5.6.1、set_intersection

    功能描述:求两个容器的交集。

    函数原型:

    • set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

      //求两个集合的交集

      //注意:两个集合必须是有序序列。

      //beg1:容器1开始迭代器;end1:容器1结束迭代器

      //beg2:容器2开始迭代器;end2:容器2结束迭代器

      //dest 目标容器开始迭代器

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用集合算法-set_union
    6. void myPrint(int val) {
    7. cout << val << " ";
    8. }
    9. class myPrint {
    10. public:
    11. void operator()(int val) {
    12. cout << val << " ";
    13. }
    14. };
    15. void test01() {
    16. vector<int> v1;
    17. vector<int> v2;
    18. for (int i = 0; i < 10; i++) {
    19. v1.push_back(i);
    20. v2.push_back(i + 5);
    21. }
    22. vector<int> vTarget;
    23. //目标容器提前开辟空间
    24. //最特殊情况:两个容器没有交集;并集就是两个容器size相加
    25. vTarget.resize(v1.size() + v2.size());//取两个容器的和给目标容器开辟空间
    26. //返回目标容器的最后一个元素的迭代器地址
    27. vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    28. for_each(vTarget.begin(), itEnd, myPrint);
    29. cout << endl;
    30. }
    31. int main() {
    32. test01();
    33. system("pause");
    34. return 0;
    35. }

    总结:

    1. 求交集的两个集合必须的有序序列;

    2. 目标容器开辟空间需要从两个容器中取小值

    3. set_intersection返回值既是交集中最后一个元素的位置。

    5.6.2、set_union

    功能描述:求两个集合的并集。

    函数原型:

    • set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

      //求两个集合的并集

      //注意:两个集合必须是有序序列

      //beg1:容器1开始迭代器;end1:容器1结束迭代器

      //beg2:容器2开始迭代器;end2:容器2结束迭代器

      //dest:目标容器开始迭代器

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用集合算法-set_union
    6. void myPrint(int val) {
    7. cout << val << " ";
    8. }
    9. class myPrint {
    10. public:
    11. void operator()(int val) {
    12. cout << val << " ";
    13. }
    14. };
    15. void test01() {
    16. vector<int> v1;
    17. vector<int> v2;
    18. for (int i = 0; i < 10; i++) {
    19. v1.push_back(i);
    20. v2.push_back(i + 5);
    21. }
    22. vector<int> vTarget;
    23. //目标容器提前开辟空间
    24. //最特殊情况:两个容器没有交集;并集就是两个容器size相加
    25. vTarget.resize(v1.size() + v2.size());//取两个容器的和给目标容器开辟空间
    26. //返回目标容器的最后一个元素的迭代器地址
    27. vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    28. for_each(vTarget.begin(), itEnd, myPrint);
    29. cout << endl;
    30. }
    31. int main() {
    32. test01();
    33. system("pause");
    34. return 0;
    35. }

    总结:

    1. 求并集的两个集合必须的有序序列;

    2. 目标容器开辟空间需要两个容器相加

    3. set_union返回值既是并集中最后一个元素的位置。

    5.6.3、set_difference

    功能描述:求两个集合的差集。

    函数原型:

    • set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

      //求两个集合的差集

      //注意:两个集合必须是有序序列

      //beg1:容器1开始迭代器;end1:容器1结束迭代器

      //beg2:容器2开始迭代器;end2:容器2结束迭代器

      //dest 目标容器开始迭代器

    示例:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //常用集合算法-set_difference
    6. void myPrint(int val) {
    7. cout << val << " ";
    8. }
    9. class myPrint {
    10. public:
    11. void operator()(int val) {
    12. cout << val << " ";
    13. }
    14. };
    15. void test01() {
    16. vector<int> v1;
    17. vector<int> v2;
    18. for (int i = 0; i < 10; i++) {
    19. v1.push_back(i);
    20. v2.push_back(i + 5);
    21. }
    22. //创建目标容器
    23. vector<int> vTarget;
    24. //给目标容器开辟空间
    25. //最特殊情况 两个容器没有交集 取两个容器中大的size作为目标容器开辟空间
    26. vTarget.resize(max(v1.size(), v2.size()));//取两个里面较大的值给目标容器开辟空间
    27. //返回目标容器的最后一个元素的迭代器地址
    28. cout << "v1和v2的差集为:" << endl;
    29. vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    30. for_each(vTarget.begin(), itEnd, myPrint);
    31. cout << endl;
    32. cout << "v2和v1的差集为:" << endl;
    33. itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
    34. for_each(vTarget.begin(), itEnd, myPrint);
    35. cout << endl;
    36. }
    37. int main() {
    38. test01();
    39. system("pause");
    40. return 0;
    41. }

    总结:

    1. 求差集的两个集合必须的有序序列;

    2. 目标容器开辟空间需要从两个容器取较大值

    3. set_difference返回值既是差集中最后一个元素的位置。

  • 相关阅读:
    卷积神经网络简介
    iterm2 配置自动登录跳板机,无需输入密码和google验证码
    TPAMI 2022 | 自动搜索文本识别网络的高性能特征提取器
    Python中的pymysql模块连接MySQL数据库,并创建表和插入数据
    SpringBoot Actuator监控组件笔记
    DOM树的遍历-----修改样式,选择元素,创建和删除节点
    小米为什么造不出芯片
    (web前端网页制作课作业)使用HTML+CSS制作非物质文化遗产专题网页设计与实现
    SpringBoot一站式功能提供框架(二)Mybatis Plus分页、Websocket 消息推送、提取word--柚子真好吃
    伊始:「深入浅出」的学习
  • 原文地址:https://blog.csdn.net/weixin_44949135/article/details/126445004