• STL - 常用算法


    概述:

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

    5.1. 常用遍历算法

    学习目标:

    • 掌握常用的遍历算法

    算法简介:

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

    5.1.1. for_each

    功能描述:

    • 实现遍历容器
    • 在实际开发中最常用的遍历算法,必须熟练掌握

    函数原型:

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

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

    // beg 开始迭代器

    // end 结束迭代器

    // _func 函数或者函数对象

    1. #include<iostream>
    2. using namespace std;
    3. #include<vector>
    4. #include<algorithm>
    5. // 普通函数
    6. void Print01(int val)
    7. {
    8. cout << val << ' ';
    9. }
    10. // 仿函数
    11. class Print02
    12. {
    13. public:
    14. void operator()(int val)
    15. {
    16. cout << val << ' ';
    17. }
    18. };
    19. void Test01()
    20. {
    21. vector<int> v;
    22. for(int i = 0; i < 10; i++)
    23. {
    24. v.push_back(i);
    25. }
    26. for_each(v.begin(), v.end(), Print01);
    27. cout << endl;
    28. for_each(v.begin(), v.end(), Print02());
    29. cout << endl;
    30. }
    31. int main()
    32. {
    33. Test01();
    34. return 0;
    35. }
    36. 输出:
    37. -----------------------------------------------------------------------------------------
    38. 0 1 2 3 4 5 6 7 8 9
    39. 0 1 2 3 4 5 6 7 8 9

    5.1.2. transform

    功能描述:

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

    函数原型:

    • transform(iterator begl, iterator endl, iterator beg2, _func);

    //beg1 源容器开始迭代器

    //end1 源容器结束迭代器

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

    //_func 函数或者函数对象

    1. #include<iostream>
    2. using namespace std;
    3. #include<vector>
    4. #include<algorithm>
    5. // 仿函数
    6. class Transform
    7. {
    8. public:
    9. int operator()(int val)
    10. {
    11. return val + 100;
    12. }
    13. };
    14. class MyPrint
    15. {
    16. public:
    17. void operator()(int val)
    18. {
    19. cout << val << ' ';
    20. }
    21. };
    22. void Test01()
    23. {
    24. vector<int> v;
    25. for(int i = 0; i < 10; i++)
    26. {
    27. v.push_back(i);
    28. }
    29. vector<int> vTarget;
    30. vTarget.resize(v.size()); // 目标容器需要提前开辟空间
    31. transform(v.begin(), v.end(), vTarget.begin(), Transform());
    32. for_each(vTarget.begin(), vTarget.end(), MyPrint());
    33. cout << endl;
    34. }
    35. int main()
    36. {
    37. Test01();
    38. return 0;
    39. }
    40. 输出:
    41. -----------------------------------------------------------------------------------------
    42. 100 101 102 103 104 105 106 107 108 109

    5.2. 常用查找算法

    学习目标:

    • 掌握常用的查找算法

    算法简介:

    • find //查找元素
    • find_if //按条件查找元素
    • adjacent_find //查找相邻重复元素
    • binary_search //二分查找法
    • count //统计元素个数
    • count_if //按条件统计元素个数

    5.2.1. find

    功能描述:

    • 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()
    • 利用find可以在容器中找到指定的元素,返回值是迭代器

    函数原型:

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

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

    // beg 开始迭代器

    // end 结束迭代器

    // value 查找的元素

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. // 查找内置数据类型
    7. void Test01()
    8. {
    9. vector<int> v;
    10. for(int i = -5; i < 10; i++)
    11. {
    12. v.push_back(i);
    13. }
    14. vector<int>::iterator it = find(v.begin(), v.end(), 5);
    15. if (it == v.end())
    16. {
    17. cout << "not find!" << endl;
    18. }
    19. else
    20. {
    21. cout << "find-" << *it << endl;
    22. }
    23. }
    24. // 查找自定义数据类型
    25. class Person
    26. {
    27. public:
    28. Person(string name, int age) : m_name(name), m_age(age){}
    29. // 重载 == ,让底层的find知道如何对比person数据类型
    30. bool operator==(const Person & p)
    31. {
    32. if(this->m_name == p.m_name && this->m_age == p.m_age)
    33. {
    34. return true;
    35. }
    36. else
    37. {
    38. return false;
    39. }
    40. }
    41. string m_name;
    42. int m_age;
    43. };
    44. void Test02()
    45. {
    46. vector<Person> v;
    47. Person p1("aaa", 10);
    48. Person p2("bbb", 20);
    49. Person p3("ccc", 30);
    50. Person p4("ddd", 40);
    51. v.push_back(p1);
    52. v.push_back(p2);
    53. v.push_back(p3);
    54. v.push_back(p4);
    55. vector<Person>::iterator it = find(v.begin(), v.end(), p2);
    56. if (it == v.end())
    57. {
    58. cout << "not find!" << endl;
    59. }
    60. else
    61. {
    62. cout << "find-name:" << it->m_name << " find-age:" << it->m_age << endl;
    63. }
    64. }
    65. int main()
    66. {
    67. Test01();
    68. Test02();
    69. return 0;
    70. }
    71. 输出:
    72. -----------------------------------------------------------------------------------------
    73. find-5
    74. find-name:bbb find-age:20

    5.2.2. find_if

    功能描述:

    • 按条件查找元素

    函数原型:

    • find _if(iterator beg, iterator end, _pred);

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

    // beg 开始迭代器

    // end 结束迭代器

    // _Pred 函数或者谓词(返回bool类型的仿函数)

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. class GreaterFive
    7. {
    8. public:
    9. bool operator()(int val)
    10. {
    11. return val > 5;
    12. }
    13. };
    14. class Person
    15. {
    16. public:
    17. Person(string name, int age) : m_name(name), m_age(age){}
    18. string m_name;
    19. int m_age;
    20. };
    21. // 查找内置数据类型
    22. void Test01()
    23. {
    24. vector<int> v;
    25. for(int i = 0; i < 10; i++)
    26. {
    27. v.push_back(i);
    28. }
    29. vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
    30. if(it == v.end())
    31. {
    32. cout << "没有找到" << endl;
    33. }
    34. else
    35. {
    36. cout << "找到了大于5的数字为:" << *it << endl;
    37. }
    38. }
    39. class Greater20
    40. {
    41. public:
    42. bool operator()(Person &p)
    43. {
    44. return p.m_age > 20;
    45. }
    46. };
    47. void Test02()
    48. {
    49. vector<Person> v;
    50. Person p1("孙悟空", 100);
    51. Person p2("猪八戒", 80);
    52. Person p3("沙僧", 30);
    53. Person p4("唐僧", 10);
    54. v.push_back(p1);
    55. v.push_back(p2);
    56. v.push_back(p3);
    57. v.push_back(p4);
    58. vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
    59. if(it == v.end())
    60. {
    61. cout << "没有找到" << endl;
    62. }
    63. else
    64. {
    65. cout << "找到了大于20岁的人:" << it->m_name << " " << it->m_age << endl;
    66. }
    67. }
    68. int main()
    69. {
    70. Test01();
    71. Test02();
    72. return 0;
    73. }
    74. 输出:
    75. -----------------------------------------------------------------------------------------
    76. 找到了大于5的数字为:6
    77. 找到了大于20岁的人:孙悟空 100

    5.2.3. adjacent_find

    功能描述:

    • 查找相邻重复元素

    函数原型:

    • adjacent find(iterator beg, iterator end);

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

    // beg 开始迭代器

    // end 结束迭代器

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. // 查找内置数据类型
    7. void Test01()
    8. {
    9. vector<int> v;
    10. v.push_back(0);
    11. v.push_back(2);
    12. v.push_back(4);
    13. v.push_back(6);
    14. v.push_back(6);
    15. v.push_back(8);
    16. v.push_back(10);
    17. v.push_back(10);
    18. vector<int>::iterator it = adjacent_find(v.begin(), v.end());
    19. if(it == v.end())
    20. {
    21. cout << "没有找到" << endl;
    22. }
    23. else
    24. {
    25. cout << "找到的第一个相邻重复元素为:" << *it << endl;
    26. }
    27. }
    28. int main()
    29. {
    30. Test01();
    31. return 0;
    32. }
    33. 输出:
    34. -----------------------------------------------------------------------------------------
    35. 找到的相邻重复元素为:6

    5.2.4. binary_search

    功能描述:

    • 查找指定元素是否存在
    • 二分查找,指数级运算,速度非常快
    • 二分查找法查找效率很高,值得注意的是查找的容器中的元素必须是有序序列,否则结果不一定对

    函数原型:

    • bool binary search(iterator beg, iterator end, value);

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

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

    // beg 开始迭代器

    // end 结束迭代器

    // value 查找的元素

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. // 查找内置数据类型
    7. void Test01()
    8. {
    9. vector<int> v;
    10. v.push_back(0);
    11. v.push_back(2);
    12. v.push_back(4);
    13. v.push_back(6);
    14. v.push_back(6);
    15. v.push_back(8);
    16. v.push_back(10);
    17. v.push_back(10);
    18. bool ret = binary_search(v.begin(), v.end(), 9);
    19. if(!ret)
    20. {
    21. cout << "没有找到" << endl;
    22. }
    23. else
    24. {
    25. cout << "成功找到" << endl;
    26. }
    27. }
    28. int main()
    29. {
    30. Test01();
    31. return 0;
    32. }
    33. 输出:
    34. -----------------------------------------------------------------------------------------
    35. 没有找到

    5.2.5. count

    功能描述:

    • 统计元素个数

    函数原型:

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

    // 统计元素出现次数

    // beg 开始迭代器

    // end 结束迭代器

    // value 统计的元素

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. class Person
    7. {
    8. public:
    9. Person(string name, int age) : m_name(name), m_age(age){}
    10. bool operator==(const Person & p)
    11. {
    12. if(this->m_age == p.m_age)
    13. {
    14. return true;
    15. }
    16. else
    17. {
    18. return false;
    19. }
    20. }
    21. string m_name;
    22. int m_age;
    23. };
    24. // 查找内置数据类型
    25. void Test01()
    26. {
    27. vector<int> v;
    28. v.push_back(0);
    29. v.push_back(2);
    30. v.push_back(4);
    31. v.push_back(6);
    32. v.push_back(6);
    33. v.push_back(8);
    34. v.push_back(10);
    35. v.push_back(10);
    36. v.push_back(10);
    37. int num = count(v.begin(), v.end(), 10);
    38. cout << "10的元素个数为:" << num << endl;
    39. }
    40. void Test02()
    41. {
    42. vector<Person> v;
    43. Person p1("刘备", 35);
    44. Person p2("关羽", 35);
    45. Person p3("张飞", 35);
    46. Person p4("赵云", 30);
    47. Person p5("曹操", 44);
    48. Person p("诸葛亮", 35);
    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. int num = count(v.begin(), v.end(), p);
    55. cout << "和诸葛亮同岁的人有:" << num << "个" << endl;
    56. }
    57. int main()
    58. {
    59. Test01();
    60. Test02();
    61. return 0;
    62. }
    63. 输出:
    64. -----------------------------------------------------------------------------------------
    65. 10的元素个数为:3
    66. 和诸葛亮同岁的人有:3

    5.2.6. count_if

    功能描述:

    • 按条件统计元素个数

    函数原型:

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

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

    // beg 开始迭代器

    // end 结束迭代器

    // _Pred 谓词

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. class Person
    7. {
    8. public:
    9. Person(string name, int age) : m_name(name), m_age(age){}
    10. bool operator==(const Person & p)
    11. {
    12. if(this->m_age == p.m_age)
    13. {
    14. return true;
    15. }
    16. else
    17. {
    18. return false;
    19. }
    20. }
    21. string m_name;
    22. int m_age;
    23. };
    24. class Greater20
    25. {
    26. public:
    27. bool operator()(int v1)
    28. {
    29. return v1 > 20;
    30. }
    31. };
    32. // 查找内置数据类型
    33. void Test01()
    34. {
    35. vector<int> v;
    36. v.push_back(0);
    37. v.push_back(2);
    38. v.push_back(4);
    39. v.push_back(6);
    40. v.push_back(6);
    41. v.push_back(8);
    42. v.push_back(10);
    43. v.push_back(30);
    44. v.push_back(50);
    45. int num = count_if(v.begin(), v.end(), Greater20());
    46. cout << "大于20的元素个数为:" << num << endl;
    47. }
    48. class GreaterPerson
    49. {
    50. public:
    51. bool operator()(const Person & p)
    52. {
    53. return p.m_age > 20;
    54. }
    55. };
    56. void Test02()
    57. {
    58. vector<Person> v;
    59. Person p1("刘备", 35);
    60. Person p2("关羽", 35);
    61. Person p3("张飞", 35);
    62. Person p4("赵云", 30);
    63. Person p5("曹操", 44);
    64. v.push_back(p1);
    65. v.push_back(p2);
    66. v.push_back(p3);
    67. v.push_back(p4);
    68. v.push_back(p5);
    69. int num = count_if(v.begin(), v.end(), GreaterPerson());
    70. cout << "年龄大于20的人数:" << num << endl;
    71. }
    72. int main()
    73. {
    74. Test01();
    75. Test02();
    76. return 0;
    77. }
    78. 输出:
    79. -----------------------------------------------------------------------------------------
    80. 大于20的元素个数为:2
    81. 年龄大于20的人数:5

    5.3. 常用排序算法

    学习目标:

    • 掌握常用的排序算法

    算法简介:

    • sort // 对容器内元素进行排序
    • random_shuffle // 洗牌,指定范围内的元素随机调整次序
    • merge // 容器元素合并,并存储到另一容器中
    • reverse // 反转指定范围的元素

    5.3.1. sort

    功能描述:

    • 对容器内元素进行排序(默认从小到大)

    函数原型:

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

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

    // beg 开始迭代器

    // end 结束迭代器

    // _Pred 谓词

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. void MyPrint(int val)
    7. {
    8. cout << val << " ";
    9. }
    10. // 查找内置数据类型
    11. void Test01()
    12. {
    13. vector<int> v;
    14. v.push_back(1);
    15. v.push_back(3);
    16. v.push_back(5);
    17. v.push_back(2);
    18. v.push_back(4);
    19. v.push_back(9);
    20. v.push_back(7);
    21. sort(v.begin(), v.end());
    22. for_each(v.begin(), v.end(), MyPrint);
    23. cout << endl;
    24. sort(v.begin(), v.end(), greater<int>());
    25. for_each(v.begin(), v.end(), MyPrint);
    26. cout << endl;
    27. }
    28. int main()
    29. {
    30. Test01();
    31. return 0;
    32. }
    33. 输出:
    34. -----------------------------------------------------------------------------------------
    35. 1 2 3 4 5 7 9
    36. 9 7 5 4 3 2 1

    5.3.2. random_shuffle

    功能描述:

    • 洗牌,指定范围内的元素随机调整次序
    • 若想每次都不一样,要加随机数种子

    函数原型:

    • random_shuffle(iterator_beg, iterator_end);

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

    // beg 开始迭代器

    // end 结束迭代器

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. #include<ctime>
    7. void MyPrint(int val)
    8. {
    9. cout << val << " ";
    10. }
    11. void Test01()
    12. {
    13. srand((unsigned int) time (NULL));
    14. vector<int> v;
    15. for (int i = 0; i < 10; i++)
    16. {
    17. v.push_back(i);
    18. }
    19. random_shuffle(v.begin(), v.end());
    20. for_each(v.begin(), v.end(), MyPrint);
    21. cout << endl;
    22. random_shuffle(v.begin(), v.end());
    23. for_each(v.begin(), v.end(), MyPrint);
    24. cout << endl;
    25. }
    26. int main()
    27. {
    28. Test01();
    29. return 0;
    30. }
    31. 输出:
    32. -----------------------------------------------------------------------------------------
    33. 0 8 6 4 5 1 7 2 3 9
    34. 0 3 1 2 4 6 8 9 7 5

    5.3.3. merge

    功能描述:

    • 两个容器元素合并,并存储到另一容器中
    • 目标容器需要提前开辟空间
    • 合并后的序列还是一个有序序列

    函数原型:

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

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

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

    // beg1 容器1开始迭代器

    // end1 容器1结束迭代器

    // beg2 容器2开始迭代器

    // end2 容器2结束迭代器

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

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. void MyPrint(int val)
    7. {
    8. cout << val << " ";
    9. }
    10. // 查找内置数据类型
    11. void Test01()
    12. {
    13. vector<int> v1;
    14. vector<int> v2;
    15. for(int i = 0; i < 10; i++)
    16. {
    17. v1.push_back(i);
    18. v2.push_back(i+1);
    19. }
    20. vector<int> v3;
    21. v3.resize(v1.size() + v2.size());
    22. merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
    23. for_each(v3.begin(), v3.end(), MyPrint);
    24. cout << endl;
    25. }
    26. int main()
    27. {
    28. Test01();
    29. return 0;
    30. }
    31. 输出:
    32. -----------------------------------------------------------------------------------------
    33. 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10

    5.3.4. reverse

    功能描述:

    • 将容器内元素进行反转

    函数原型:

    • reverse(iterator beg,iterator end);

    // 反转指定范围的元素

    // beg 开始迭代器

    // end 结束迭代器

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. void MyPrint(int val)
    7. {
    8. cout << val << " ";
    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. reverse(v.begin(), v.end());
    19. for_each(v.begin(), v.end(), MyPrint);
    20. cout << endl;
    21. }
    22. int main()
    23. {
    24. Test01();
    25. return 0;
    26. }
    27. 输出:
    28. -----------------------------------------------------------------------------------------
    29. 9 8 7 6 5 4 3 2 1 0

    5.4. 常用拷贝和替换算法

    学习目标:

    • 掌握常用的拷贝和替换算法

    算法简介:

    • copy // 容器内指定范围的元素拷贝到另一容器中
    • replace // 将容器内指定范围的旧元素修改为新元素
    • replace_if // 容器内指定范围满足条件的元素替换为新元素
    • swap // 互换两个容器的元素

    5.4.1. copy

    功能描述:

    • 容器内指定范围的元素拷贝到另一容器中
    • 目标容器要提前开辟空间

    函数原型:

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

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

    // beg 开始迭代器

    // end 结束迭代器

    // dest 目标起始迭代器

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. void MyPrint(int val)
    7. {
    8. cout << val << " ";
    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> v2;
    19. v2.resize(v.size());
    20. copy(v.begin(), v.end(), v2.begin());
    21. for_each(v2.begin(), v2.end(), MyPrint);
    22. cout << endl;
    23. }
    24. int main()
    25. {
    26. Test01();
    27. return 0;
    28. }
    29. 输出:
    30. -----------------------------------------------------------------------------------------
    31. 0 1 2 3 4 5 6 7 8 9

    5.4.2. replace

    功能描述:

    • 将容器内指定范围所有的旧元素修改为新元素

    函数原型:

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

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

    // beg 开始迭代器

    // end 结束迭代器

    // oldvalue 旧元素

    // newvalue 新元素

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. class MyPrint
    7. {
    8. public:
    9. void operator()(int val)
    10. {
    11. cout << val << " ";
    12. }
    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. replace(v.begin(), v.end(), 3, 100);
    23. for_each(v.begin(), v.end(), MyPrint());
    24. cout << endl;
    25. }
    26. int main()
    27. {
    28. Test01();
    29. return 0;
    30. }
    31. 输出:
    32. -----------------------------------------------------------------------------------------
    33. 0 1 2 100 4 5 6 7 8 9

    5.4.3. replace_if

    功能描述:

    • 将区间内满足条件的元素,替换成指定元素

    函数原型:

    • replace if(iterator beg, iterator end, _pred, newvalue);

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

    // beg 开始迭代器

    // end 结束迭代器

    // _pred 谓词

    // newvalue 替换的新元素

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. class Greater5
    7. {
    8. public:
    9. bool operator()(int val)
    10. {
    11. return val > 5;
    12. }
    13. };
    14. class MyPrint
    15. {
    16. public:
    17. void operator()(int val)
    18. {
    19. cout << val << " ";
    20. }
    21. };
    22. // 查找内置数据类型
    23. void Test01()
    24. {
    25. vector<int> v;
    26. for(int i = 0; i < 10; i++)
    27. {
    28. v.push_back(i);
    29. }
    30. replace_if(v.begin(), v.end(), Greater5(), 100);
    31. for_each(v.begin(), v.end(), MyPrint());
    32. cout << endl;
    33. }
    34. int main()
    35. {
    36. Test01();
    37. return 0;
    38. }
    39. 输出:
    40. -----------------------------------------------------------------------------------------
    41. 0 1 2 3 4 5 100 100 100 100

    5.4.4. swap

    功能描述:

    • 互换两个容器的元素

    函数原型:

    • swap(container c1,container c2);

    // 互换两个容器的元素

    // c1容器1

    // c2容器2

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. class MyPrint
    7. {
    8. public:
    9. void operator()(int val)
    10. {
    11. cout << val << " ";
    12. }
    13. };
    14. // 查找内置数据类型
    15. void Test01()
    16. {
    17. vector<int> v1;
    18. vector<int> v2;
    19. for(int i = 0; i < 10; i++)
    20. {
    21. v1.push_back(i);
    22. v2.push_back(i+100);
    23. }
    24. cout << "交换前:" << endl;
    25. for_each(v1.begin(), v1.end(), MyPrint());
    26. for_each(v2.begin(), v2.end(), MyPrint());
    27. cout << endl;
    28. swap(v1, v2);
    29. cout << "交换后:" << endl;
    30. for_each(v1.begin(), v1.end(), MyPrint());
    31. for_each(v2.begin(), v2.end(), MyPrint());
    32. cout << endl;
    33. }
    34. int main()
    35. {
    36. Test01();
    37. return 0;
    38. }
    39. 输出:
    40. -----------------------------------------------------------------------------------------
    41. 交换前:
    42. 0 1 2 3 4 5 6 7 8 9 100 101 102 103 104 105 106 107 108 109
    43. 交换后:
    44. 100 101 102 103 104 105 106 107 108 109 0 1 2 3 4 5 6 7 8 9

    5.5. 常用算术生成算法

    学习目标:

    • 掌握常用的算术生成算法

    注意:

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

    算法简介:

    • accumulate // 计算容器元素累计总和
    • fill // 向容器中添加元素

    5.5.1. accumulate

    功能描述:

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

    函数原型:

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

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

    // beg 开始迭代器

    // end 结束迭代器

    // value 起始值

    1. #include<iostream>
    2. using namespace std;
    3. #include<vector>
    4. #include<numeric>
    5. // 查找内置数据类型
    6. void Test01()
    7. {
    8. vector<int> v;
    9. for(int i = 0; i <= 100; i++)
    10. {
    11. v.push_back(i);
    12. }
    13. int total = accumulate(v.begin(), v.end(), 0);
    14. cout << total << endl;
    15. }
    16. int main()
    17. {
    18. Test01();
    19. return 0;
    20. }
    21. 输出:
    22. -----------------------------------------------------------------------------------------
    23. 5050

    5.5.2. fill

    功能描述:

    • 向容器中填充指定的元素

    函数原型:

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

    // 向容器中填充元素

    // beg 开始迭代器

    // end 结束迭代器

    // value 填充的值

    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. #include<numeric>
    7. class MyPrint
    8. {
    9. public:
    10. void operator()(int val)
    11. {
    12. cout << val << " ";
    13. }
    14. };
    15. void Test01()
    16. {
    17. vector<int> v;
    18. // 初始化容器大小为10,并全部填充为0
    19. v.resize(10);
    20. for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
    21. {
    22. cout << *it << ' ';
    23. }
    24. cout << endl;
    25. // 重新填充
    26. fill(v.begin(), v.end(), 100);
    27. for_each(v.begin(), v.end(), MyPrint());
    28. }
    29. int main()
    30. {
    31. Test01();
    32. return 0;
    33. }
    34. 输出:
    35. -----------------------------------------------------------------------------------------
    36. 0 0 0 0 0 0 0 0 0 0
    37. 100 100 100 100 100 100 100 100 100 100

    5.6. 常用集合算法

    学习目标:

    • 掌握常用的集合算法

    算法简介:

    • set_intersection // 求两个容器的交集
    • set_union // 求两个容器的并集
    • set_difference // 求两个容器的差集

    5.6.1. set_intersection

    功能描述:

    • 求两个容器的交集

    函数原型:

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

    // 求两个集合的交集

    // beg1 容器1开始迭代器

    // end1 容器1结束迭代器

    // beg2 容器2开始迭代器

    // end2 容器2结束迭代器

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

    总结:

    • 求交集的两个集合必须是有序序列
    • 目标容器开辟的空间为两个容器中size最小的哪一个
    • set_intersection的返回值是交集迭代器中最后一个元素的位置
    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. #include<numeric>
    7. class MyPrint
    8. {
    9. public:
    10. void operator()(int val)
    11. {
    12. cout << val << " ";
    13. }
    14. };
    15. void Test01()
    16. {
    17. vector<int> v1;
    18. vector<int> v2;
    19. for(int i = 0; i < 10; i++)
    20. {
    21. v1.push_back(i); // 0~9
    22. v2.push_back(i + 5); // 5~14
    23. }
    24. vector<int> vTarget;
    25. // 目标容器需要提前开辟空间
    26. // 最特殊情况:大容器完全包含小容器,开辟空间时取小容器的size即可
    27. vTarget.resize(min(v1.size(), v2.size()));
    28. // 获取交集
    29. vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    30. for_each(vTarget.begin(), vTarget.end(), MyPrint());
    31. cout << endl;
    32. for_each(vTarget.begin(), itEnd, MyPrint());
    33. }
    34. int main()
    35. {
    36. Test01();
    37. return 0;
    38. }
    39. 输出:
    40. -----------------------------------------------------------------------------------------
    41. 5 6 7 8 9 0 0 0 0 0
    42. 5 6 7 8 9

    5.6.2. set_union

    功能描述:

    • 求两个集合的并集

    函数原型:

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

    // 求两个集合的并集

    // beg1 容器1开始迭代器

    // end1 容器1结束迭代器

    // beg2 容器2开始迭代器

    // end2 容器2结束迭代器

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

    总结:

    • 求并集的两个集合必须是有序序列
    • 目标容器开辟的空间为两个容器的size相加
    • set_intersection的返回值是交集迭代器中最后一个元素的位置
    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. #include<numeric>
    7. class MyPrint
    8. {
    9. public:
    10. void operator()(int val)
    11. {
    12. cout << val << " ";
    13. }
    14. };
    15. void Test01()
    16. {
    17. vector<int> v1;
    18. vector<int> v2;
    19. for(int i = 0; i < 10; i++)
    20. {
    21. v1.push_back(i); // 0~9
    22. v2.push_back(i + 5); // 5~14
    23. }
    24. vector<int> vTarget;
    25. // 目标容器需要提前开辟空间
    26. // 最特殊情况:大容器与小容器完全不重合,开辟空间时取两个容器size之和
    27. vTarget.resize(v1.size() + v2.size());
    28. // 获取并集
    29. vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    30. for_each(vTarget.begin(), vTarget.end(), MyPrint());
    31. cout << endl;
    32. for_each(vTarget.begin(), itEnd, MyPrint());
    33. }
    34. int main()
    35. {
    36. Test01();
    37. return 0;
    38. }
    39. 输出:
    40. -----------------------------------------------------------------------------------------
    41. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 0 0 0 0
    42. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    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 目标容器开始迭代器

    总结:

    • 求并集的两个集合必须是有序序列
    • 目标容器开辟的空间为两个容器的size相加
    • set_intersection的返回值是交集迭代器中最后一个元素的位置
    1. #include<iostream>
    2. using namespace std;
    3. #include<string>
    4. #include<vector>
    5. #include<algorithm>
    6. #include<numeric>
    7. class MyPrint
    8. {
    9. public:
    10. void operator()(int val)
    11. {
    12. cout << val << " ";
    13. }
    14. };
    15. void Test01()
    16. {
    17. vector<int> v1;
    18. vector<int> v2;
    19. for(int i = 0; i < 10; i++)
    20. {
    21. v1.push_back(i); // 0~9
    22. v2.push_back(i + 5); // 5~14
    23. }
    24. vector<int> vTarget;
    25. // 目标容器需要提前开辟空间
    26. // 最特殊情况:大容器与小容器完全不重合,开辟空间时取两个容器size之和
    27. vTarget.resize(max(v1.size(), v2.size()));
    28. // 获取并集
    29. vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    30. cout << "v1和v2的差集为:" << endl;
    31. for_each(vTarget.begin(), itEnd, MyPrint());
    32. cout << endl;
    33. cout << "v2和v1的差集为:" << endl;
    34. itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
    35. for_each(vTarget.begin(), itEnd, MyPrint());
    36. }
    37. int main()
    38. {
    39. Test01();
    40. return 0;
    41. }
    42. 输出:
    43. -----------------------------------------------------------------------------------------
    44. v1和v2的差集为:
    45. 0 1 2 3 4
    46. v2和v1的差集为:
    47. 10 11 12 13 14
  • 相关阅读:
    游戏服务器搭建过程中Maven多模块编译遇到的一些问题
    SpringBoot整合JWT
    ModbusTCP 转 Profinet 主站网关在博图配置案例
    【每日一题Day337】LC460LFU 缓存 | 双链表+哈希表
    【Linux】详解自主实现HTTP服务器
    K-近邻算法分类和回归
    【CANoe/CANalyzer脚本】通过CAPL发送NM帧报文测试网络管理
    FS4061A(5V USB输入、双节锂电池串联应用、5v升压充电8.4v管理IC
    python基于PHP+MySQL的校园帮忙领取快递平台
    关于接口测试问题,这些文章推荐你反复看看
  • 原文地址:https://blog.csdn.net/qq_53795212/article/details/139663800