• 详解c++STL—STL常用算法


    目录

    1、常用遍历算法

    1.1、for_each

    1.2、transform

    2、常用查找算法

    2.1、find

    2.2、find_if

    2.3、adjacent_find

    2.4、binary_search

    2.5、count

    2.6、count_if

    3、常用排序算法

    3.1、sort

    3.2、random_shuffle

    3.3、merge

    3.4、reverse

    4、常用拷贝和替换算法

    4.1、copy

    4.2、replace

    4.3、replace_if

    4.4、swap

    5、常用算术生成算法

    5.1、accumulate

    5.2、fill

    6、常用集合算法

    6.1、set_intersection

    6.2、set_union

    6.3、set_difference


    概述:

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

    1、常用遍历算法

    1.1、for_each

    功能描述:

    • 实现容器的遍历

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // _func 函数或者函数对象

    示例:

    1. #include
    2. #include
    3. //常用遍历算法 for_each
    4. void print01(int val) {
    5. cout << val << " ";
    6. }
    7. class Print02 {
    8. public:
    9. void operator()(int val) const {
    10. cout << val<<" ";
    11. }
    12. };
    13. void test01() {
    14. int a[] = { 1,2,4,5,6,7,8,0,9 };
    15. vector<int> v(a,a+9);
    16. //用函数名,作为第三个参数
    17. for_each(v.begin(), v.end(),print01);
    18. cout << endl;
    19. //用仿函数,作为第三个参数
    20. //Print02() 匿名对象
    21. for_each(v.begin(), v.end(),Print02());
    22. cout << endl;
    23. }
    24. int main() {
    25. test01();
    26. system("pause");
    27. return 0;
    28. }

    1.2、transform

    功能描述:

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

    函数原型:

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

    //beg1 源容器开始迭代器

    //end1 源容器结束迭代器

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

    //_func 函数或者函数对象

    示例:

    1. //常用遍历算法 transform
    2. class Print02 {
    3. public:
    4. int operator()(int val) const {
    5. cout << val << " ";
    6. return val;
    7. }
    8. };
    9. int func(int val) {
    10. return val;
    11. }
    12. void test01() {
    13. int a[] = { 1,2,4,5,6,7,8,0,9 };
    14. //1、源容器
    15. vector<int> vS(a, a + 9);
    16. for_each(vS.begin(), vS.end(), Print02());
    17. cout << endl;
    18. //2、目标容器
    19. vector<int> vT;
    20. vT.resize(vS.size());//开辟空间,不开辟报错、
    21. //搬运
    22. transform(vS.begin(),vS.end(),vT.begin(),func);
    23. for_each(vT.begin(),vT.end(),Print02());
    24. cout << endl;
    25. //搬运时,对_func赋予功能——打印输出
    26. transform(vS.begin(),vS.end(),vT.begin(),Print02());
    27. cout << endl;
    28. }
    29. int main() {
    30. test01();
    31. system("pause");
    32. return 0;
    33. }

    注意事项:

    1、目标容器需要提前开辟空间(resize),否则无法搬运!

    2、第四个参数(_func),用于对数据进行一定的变换或输出,如果不变换或输出,直接return

    3、第四个参数(_func),需要返回值,不可以是void

    2、常用查找算法

    2.1、find

    功能描述:

    • 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()

    函数原型:

    • find(iterator beg, iterator end, value);
    • // beg 开始迭代器
    • // end 结束迭代器
    • // value 查找的元素

    示例:

    1. //常用的查找算法 find
    2. class Print01 {
    3. public:
    4. void operator()(int val) {
    5. cout << val << " ";
    6. }
    7. };
    8. //1、查找内置数据类型
    9. void test01() {
    10. int a[] = { 1,2,6,7,8,3,4,5,9 };
    11. vector<int> v(a,a+(sizeof(a)/sizeof(a[0])));
    12. for_each(v.begin(),v.end(),Print01());
    13. cout << endl;
    14. vector<int>::iterator it = find(v.begin(),v.end(),10);
    15. if (it == v.end()) {
    16. cout << "内置数据类型 未找到" << endl;
    17. }
    18. else {
    19. cout << "内置数据类型 找到了 : " <<*it<< endl;
    20. }
    21. }
    22. //2、查找自定义数据类型
    23. class Person {
    24. public:
    25. //初始化列表,构造函数
    26. Person(string name, int age) :m_name(name), m_age(age) {}
    27. //重载 == ,让底层find知道怎么去比较
    28. bool operator==(const Person& p) {
    29. if (this->m_name == p.m_name && this->m_age == p.m_age) {
    30. return true;
    31. }
    32. else
    33. return false;
    34. }
    35. string m_name;
    36. int m_age;
    37. };
    38. class Print02 {
    39. public:
    40. void operator()(Person p) {
    41. cout << "姓名:" << p.m_name << " 年龄:" << p.m_age << endl;
    42. }
    43. };
    44. void test02() {
    45. vector vp;
    46. Person p1("aaa", 10);
    47. Person p2("bbb", 20);
    48. Person p3("ccc", 30);
    49. vp.push_back(p1);
    50. vp.push_back(p2);
    51. vp.push_back(p3);
    52. for_each(vp.begin(), vp.end(), Print02());
    53. Person pp("aaa",10);
    54. vector::iterator it1 = find(vp.begin(),vp.end(),pp);
    55. if (it1 == vp.end()) {
    56. cout << "自定义数据类型 未找到" << endl;
    57. }
    58. else {
    59. cout << "自定义数据类型 找到了 : " << endl;
    60. cout << "姓名:" << it1->m_name << " 年龄:" << it1->m_age << endl;
    61. }
    62. }
    63. int main() {
    64. test01();
    65. cout << endl;
    66. test02();
    67. system("pause");
    68. return 0;
    69. }

    注意事项:

    1、查找自定义数据类型时,需要重载==,让底层find知道如何去比较数据是否相等

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

    2.2、find_if

    功能描述:

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

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

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

    示例:

    1. //常用的查找算法 find_if
    2. class Print01 {
    3. public:
    4. void operator()(int val) {
    5. cout << val << " ";
    6. }
    7. };
    8. class greater5 {
    9. public:
    10. bool operator()(int val) {
    11. return val > 5;
    12. }
    13. };
    14. //1、查找内置数据类型
    15. void test01() {
    16. int a[] = { 1,2,6,7,8,3,4,5,9 };
    17. vector<int> v(a, a + (sizeof(a) / sizeof(a[0])));
    18. for_each(v.begin(), v.end(), Print01());
    19. cout << endl;
    20. vector<int>::iterator it = find_if(v.begin(), v.end(), greater5());
    21. if (it == v.end()) {
    22. cout << "内置数据类型 未找到" << endl;
    23. }
    24. else {
    25. cout << "内置数据类型 找到了 : " << *it << endl;
    26. }
    27. }
    28. //2、查找自定义数据类型
    29. class Person {
    30. public:
    31. //初始化列表,构造函数
    32. Person(string name, int age) :m_name(name), m_age(age) {}
    33. string m_name;
    34. int m_age;
    35. };
    36. class Print02 {
    37. public:
    38. void operator()(Person p) {
    39. cout << "姓名:" << p.m_name << " 年龄:" << p.m_age << endl;
    40. }
    41. };
    42. class greater20 {
    43. public:
    44. bool operator()(Person p) {
    45. return p.m_age > 20;
    46. }
    47. };
    48. void test02() {
    49. vector vp;
    50. Person p1("aaa", 10);
    51. Person p2("bbb", 20);
    52. Person p3("ccc", 30);
    53. vp.push_back(p1);
    54. vp.push_back(p2);
    55. vp.push_back(p3);
    56. for_each(vp.begin(), vp.end(), Print02());
    57. vector::iterator it1 = find_if(vp.begin(), vp.end(), greater20());
    58. if (it1 == vp.end()) {
    59. cout << "自定义数据类型 未找到" << endl;
    60. }
    61. else {
    62. cout << "自定义数据类型 找到了 : " << endl;
    63. cout << "姓名:" << it1->m_name << " 年龄:" << it1->m_age << endl;
    64. }
    65. }
    66. int main() {
    67. test01();
    68. cout << endl;
    69. test02();
    70. system("pause");
    71. return 0;
    72. }

    2.3、adjacent_find

    功能描述:

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

    函数原型:

    • adjacent_find(iterator beg, iterator end);

    // beg 开始迭代器

    // end 结束迭代器

    示例:

    1. //常用查找算法 adjacent_find
    2. class printVector {
    3. public:
    4. void operator()(int val) {
    5. cout << val << " ";
    6. }
    7. };
    8. void test01() {
    9. vector<int> v;
    10. v.push_back(10);
    11. v.push_back(20);
    12. v.push_back(30);
    13. v.push_back(10);
    14. v.push_back(10);
    15. for_each(v.begin(),v.end(),printVector());
    16. cout << endl;
    17. vector<int>::iterator it = adjacent_find(v.begin(),v.end());
    18. if (it == v.end()) {
    19. cout << "未找到相邻元素" << endl;
    20. }
    21. else {
    22. cout << "相邻元素:" << *it << endl;
    23. }
    24. }
    25. int main() {
    26. test01();
    27. system("pause");
    28. }

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

    功能描述:

    • 二分查找法,查找指定的元素,查到 返回true 否则false

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // value 查找的元素

    示例:

    1. //常用的查找算法 binary_search
    2. void test01() {
    3. vector<int> v;
    4. for (int i = 0; i < 10;i++) {
    5. v.push_back(i);
    6. }
    7. for (vector<int>::iterator it = v.begin(); it != v.end();it++) {
    8. cout << *it << " ";
    9. }
    10. cout << endl;
    11. bool ret = binary_search(v.begin(),v.end(),9);
    12. if (ret) {
    13. cout << "找到了9" << endl;
    14. }
    15. else
    16. cout << "未找到9" << endl;
    17. }
    18. int main(){
    19. test01();
    20. system("pause");
    21. }

    注意事项:

    1、二分查找, 仅在有序序列中可用,如果是无序序列,结果未知

    2、二分查找法的查找效率很高

    2.5、count

    功能描述:

    • 统计元素出现次数

    函数原型:

    • count(iterator beg, iterator end, value);
    • // beg 开始迭代器
    • // end 结束迭代器
    • // value 统计的元素

    示例:

    1. //常用查找算法 count(统计)
    2. //1、内置数据类型
    3. void test01() {
    4. vector<int> v;
    5. v.push_back(10);
    6. v.push_back(20);
    7. v.push_back(10);
    8. v.push_back(30);
    9. for (int i = 0; i < v.size();i++) {
    10. cout << v[i] << " ";
    11. }
    12. cout << endl;
    13. int ret = count(v.begin(),v.end(),10);
    14. cout << "自定义数据类型—10 的个数:" << ret << endl;
    15. }
    16. //2、自定义数据类型
    17. class Person {
    18. public:
    19. Person(string name, int age) {
    20. this->m_name = name;
    21. this->m_age = age;
    22. }
    23. //重载 ==
    24. bool operator==(const Person& p) {
    25. if (p.m_age == this->m_age) {
    26. return true;
    27. }
    28. else
    29. return false;
    30. }
    31. string m_name;
    32. int m_age;
    33. };
    34. void test02() {
    35. vector vp;
    36. Person p1("刘备",30);
    37. Person p2("关羽",30);
    38. Person p3("张飞",30);
    39. Person p4("小乔",20);
    40. Person p5("周瑜",40);
    41. vp.push_back(p1);
    42. vp.push_back(p2);
    43. vp.push_back(p3);
    44. vp.push_back(p4);
    45. vp.push_back(p5);
    46. Person p9("诸葛",30);
    47. vector::iterator it = vp.begin();
    48. for (int i = 0; i < vp.size();i++) {
    49. cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
    50. it++;
    51. }
    52. int ret1 = count(vp.begin(),vp.end(),p9);
    53. cout << "和诸葛年龄相同的人个数:" << ret1 << endl;
    54. }
    55. int main() {
    56. test01();
    57. cout << endl;
    58. test02();
    59. system("pause");
    60. }

    注意事项:对于统计自定义数据类型的时候,需要配合重载operator==

    2.6、count_if

    功能描述:

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

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // _Pred 谓词

    示例:

    1. //常用查找算法 count_if(条件统计)
    2. class greater5 {
    3. public:
    4. bool operator()(int val) {
    5. return val > 10;
    6. }
    7. };
    8. //1、内置数据类型
    9. void test01() {
    10. vector<int> v;
    11. v.push_back(10);
    12. v.push_back(20);
    13. v.push_back(10);
    14. v.push_back(30);
    15. for (int i = 0; i < v.size(); i++) {
    16. cout << v[i] << " ";
    17. }
    18. cout << endl;
    19. int ret = count_if(v.begin(), v.end(),greater5());
    20. cout << "内置数据类型—大于5的个数:" << ret << endl;
    21. }
    22. //2、自定义数据类型
    23. class Person {
    24. public:
    25. Person(string name, int age) {
    26. this->m_name = name;
    27. this->m_age = age;
    28. }
    29. string m_name;
    30. int m_age;
    31. };
    32. class greater20 {
    33. public:
    34. bool operator()(const Person& p) {
    35. return p.m_age > 20;
    36. }
    37. };
    38. void test02() {
    39. vector vp;
    40. Person p1("刘备", 30);
    41. Person p2("关羽", 30);
    42. Person p3("张飞", 30);
    43. Person p4("小乔", 20);
    44. Person p5("周瑜", 40);
    45. vp.push_back(p1);
    46. vp.push_back(p2);
    47. vp.push_back(p3);
    48. vp.push_back(p4);
    49. vp.push_back(p5);
    50. vector::iterator it = vp.begin();
    51. for (int i = 0; i < vp.size(); i++) {
    52. cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
    53. it++;
    54. }
    55. int ret1 = count_if(vp.begin(), vp.end(), greater20());
    56. cout << "自定义数据类型—年龄大于20:" << ret1 << endl;
    57. }
    58. int main() {
    59. test01();
    60. cout << endl;
    61. test02();
    62. system("pause");
    63. }

    3、常用排序算法

    3.1、sort

    功能描述:

    • 对容器内元素进行排序

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // _Pred 谓词

    示例:

    1. //常用排序算法 sort
    2. void print01(int val) {
    3. cout << val << " ";
    4. }
    5. class myCompera {
    6. public:
    7. bool operator()(int val1,int val2) {
    8. return val1 > val2;
    9. }
    10. };
    11. void test01() {
    12. vector<int> v;
    13. v.push_back(10);
    14. v.push_back(50);
    15. v.push_back(20);
    16. v.push_back(30);
    17. v.push_back(40);
    18. for_each(v.begin(),v.end(), print01);
    19. cout << endl;
    20. //1、默认升序
    21. sort(v.begin(),v.end());
    22. for_each(v.begin(), v.end(), print01);
    23. cout << endl;
    24. //2、使用内置函数对象,改变规则为降序
    25. sort(v.begin(),v.end(),greater<int>());
    26. for_each(v.begin(), v.end(), print01);
    27. cout << endl;
    28. //3、自定义谓词,改变排序规则
    29. sort(v.begin(), v.end(),myCompera());
    30. for_each(v.begin(), v.end(), print01);
    31. cout << endl;
    32. }
    33. int main() {
    34. test01();
    35. system("pause");
    36. return 0;
    37. }

    3.2、random_shuffle

    功能描述:

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

    函数原型:

    • random_shuffle(iterator beg, iterator end);

    // beg 开始迭代器

    // end 结束迭代器

    示例:

    1. //常用排序算法 random_shuffle
    2. void print01(int val) {
    3. cout << val << " ";
    4. }
    5. class myCompera {
    6. public:
    7. bool operator()(int val1, int val2) {
    8. return val1 > val2;
    9. }
    10. };
    11. void test01() {
    12. //随机数的种子
    13. srand((unsigned int)time(NULL));
    14. vector<int> v;
    15. for (int i = 0; i < 10;i++) {
    16. v.push_back(i);
    17. }
    18. for_each(v.begin(),v.end(),print01);
    19. cout << endl;
    20. //洗牌,打乱顺序
    21. random_shuffle(v.begin(),v.end());
    22. for_each(v.begin(), v.end(), print01);
    23. cout << endl;
    24. }
    25. int main() {
    26. test01();
    27. system("pause");
    28. return 0;
    29. }

    注意事项:如果想要多次执行单个random_shuffle,并且每次执行的打乱顺序不一致,需要加入随机数的种子

    3.3、merge

    功能描述:

    • 两个容器元素合并,并存储到另一容器中

    函数原型:

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

    // beg1 容器1开始迭代器

    // end1 容器1结束迭代器

    // beg2 容器2开始迭代器

    // end2 容器2结束迭代器

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

    示例:

    1. //常用排序算法 merge
    2. void print01(int val) {
    3. cout << val << " ";
    4. }
    5. void test01() {
    6. //1、准备两个原容器
    7. vector<int> v1;
    8. vector<int> v2;
    9. for (int i = 10; i > 0; i--) {
    10. v1.push_back(i);
    11. v2.push_back(i-1);
    12. }
    13. for_each(v1.begin(),v1.end(),print01);
    14. cout << endl;
    15. for_each(v2.begin(), v2.end(), print01);
    16. cout << endl;
    17. //2、准备一个目标容器
    18. vector<int> vt;
    19. //3、提前分配空间,否则无法执行
    20. vt.resize(v1.size() + v2.size());
    21. //5、默认升序,可设置为,降序
    22. //注意,两个原容器的排序规则必须和merge的排序规则一致,否则无法执行
    23. merge(v1.begin(),v1.end(),v2.begin(),v2.end(),vt.begin(),greater<int>());
    24. for_each(vt.begin(), vt.end(), print01);
    25. cout << endl;
    26. }
    27. int main() {
    28. test01();
    29. system("pause");
    30. }

    注意事项:

    1、 两个容器必须是有序的

    2、两个原容器的排序规则必须和merge的排序规则一致,否则无法执行(都是less,或者greater

    3.4、reverse

    功能描述:

    • 将容器内元素进行反转

    函数原型:

    • reverse(iterator beg, iterator end);

    // beg 开始迭代器

    // end 结束迭代器

    示例:

    1. //常用排序算法 reverse
    2. void test01() {
    3. vector<char> v;
    4. v.push_back('a');
    5. v.push_back('b');
    6. v.push_back('c');
    7. v.push_back('d');
    8. v.push_back('e');
    9. cout << "反转前:" << endl;
    10. for (int i = 0; i < v.size();i++) {
    11. cout << v[i] << " ";
    12. }
    13. cout << endl;
    14. //反转
    15. reverse(v.begin(),v.end());
    16. cout << "反转后:" << endl;
    17. for (int i = 0; i < v.size(); i++) {
    18. cout << v[i] << " ";
    19. }
    20. cout << endl;
    21. }
    22. int main() {
    23. test01();
    24. system("pause");
    25. }

    4、常用拷贝和替换算法

    4.1、copy

    功能描述:

    • 容器内指定范围的元素拷贝到另一容器中

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // dest 目标起始迭代器

    示例:

    1. //常用的拷贝算法 copy
    2. void myPrint(int val) {
    3. cout << val << " ";
    4. }
    5. void test01() {
    6. vector<int> v;
    7. for (int i = 0; i < 5;i++) {
    8. v.push_back(i);
    9. }
    10. for_each(v.begin(),v.end(),myPrint);
    11. cout << endl;
    12. vector<int> v2;
    13. v2.push_back(6);
    14. v2.push_back(7);
    15. v2.resize(v.size() + 2);
    16. //拷贝
    17. copy(v.begin(), v.end(),v2.begin()+2);
    18. for_each(v2.begin(), v2.end(), myPrint);
    19. cout << endl;
    20. }
    21. int main() {
    22. test01();
    23. system("pause");
    24. return 0;
    25. }

    注意事项:

    1、拷贝前,目标容器需要提前分配好空间

    2、利用copy,可以实现拼接

    4.2、replace

    功能描述:

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

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // oldvalue 旧元素

    // newvalue 新元素

    示例:

    1. //常用的替换算法 replace
    2. class myPrint {
    3. public:
    4. void operator()(int val){
    5. cout << val << " ";
    6. }
    7. };
    8. void test01() {
    9. vector<int> v;
    10. v.push_back(10);
    11. v.push_back(20);
    12. v.push_back(30);
    13. v.push_back(20);
    14. v.push_back(40);
    15. v.push_back(20);
    16. cout << "替换前" << endl;
    17. for_each(v.begin(), v.end(), myPrint());
    18. cout << endl;
    19. //将 20 ,替换为 2000
    20. replace(v.begin(),v.end(),20,2000);
    21. cout << "替换后" << endl;
    22. for_each(v.begin(), v.end(), myPrint());
    23. cout << endl;
    24. }
    25. int main() {
    26. test01();
    27. system("pause");
    28. return 0;
    29. }

    4.3、replace_if

    功能描述:

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

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // _pred 谓词

    // newvalue 替换的新元素

    示例:

    1. 常用的替换算法 replace_if
    2. class myPrint {
    3. public:
    4. void operator()(int val) {
    5. cout << val << " ";
    6. }
    7. };
    8. class greater10 {
    9. public:
    10. bool operator()(int val) {
    11. return val > 10;
    12. }
    13. };
    14. void test01() {
    15. vector<int> v;
    16. v.push_back(10);
    17. v.push_back(20);
    18. v.push_back(10);
    19. v.push_back(20);
    20. v.push_back(40);
    21. v.push_back(10);
    22. cout << "替换前" << endl;
    23. for_each(v.begin(), v.end(), myPrint());
    24. cout << endl;
    25. //将大于10的,替换为 666
    26. replace_if(v.begin(), v.end(), greater10(), 666);
    27. cout << "替换后" << endl;
    28. for_each(v.begin(), v.end(), myPrint());
    29. cout << endl;
    30. }
    31. int main() {
    32. test01();
    33. system("pause");
    34. return 0;
    35. }
    36. class myPrint {
    37. public:
    38. void operator()(int val) {
    39. cout << val << " ";
    40. }
    41. };
    42. class greater10 {
    43. public:
    44. bool operator()(int val) {
    45. return val > 10;
    46. }
    47. };
    48. void test01() {
    49. vector<int> v;
    50. v.push_back(10);
    51. v.push_back(20);
    52. v.push_back(10);
    53. v.push_back(20);
    54. v.push_back(40);
    55. v.push_back(10);
    56. cout << "替换前" << endl;
    57. for_each(v.begin(), v.end(), myPrint());
    58. cout << endl;
    59. //将大于10的,替换为 666
    60. replace_if(v.begin(), v.end(), greater10(), 666);
    61. cout << "替换后" << endl;
    62. for_each(v.begin(), v.end(), myPrint());
    63. cout << endl;
    64. }
    65. int main() {
    66. test01();
    67. system("pause");
    68. return 0;
    69. }

    4.4、swap

    功能描述:

    • 互换两个容器的元素

    函数原型:

    • swap(container c1, container c2);

    // c1 容器1

    // c2 容器2

    示例:

    1. //常用的替换算法 swap
    2. class myPrint {
    3. public:
    4. void operator()(int val) {
    5. cout << val << " ";
    6. }
    7. };
    8. void test01() {
    9. vector<int> v;
    10. vector<int> v1;
    11. for (int i = 0; i < 5;i++) {
    12. v.push_back(i);
    13. v1.push_back(i+10);
    14. }
    15. cout << "交换前:" << endl;
    16. cout << "v的地址:" << &v[0] << endl;
    17. for_each(v.begin(),v.end(),myPrint());
    18. cout << endl;
    19. cout << "v1的地址:" << &v1[0] << endl;
    20. for_each(v1.begin(),v1.end(),myPrint());
    21. cout << endl;
    22. cout << "交换后:" << endl;
    23. //实现v和v1的交换
    24. swap(v,v1);
    25. cout << "v的地址:" << &v[0] << endl;
    26. for_each(v.begin(), v.end(), myPrint());
    27. cout << endl;
    28. cout << "v1的地址:" << &v1[0] << endl;
    29. for_each(v1.begin(), v1.end(), myPrint());
    30. cout << endl;
    31. }
    32. int main() {
    33. test01();
    34. system("pause");
    35. return 0;
    36. }

    注意事项:

    1、swap交换时,发生的是地址交换

    2、swap交换时,两个容器需要同种数据类型

    5、常用算术生成算法

    5.1、accumulate

    功能描述:

    • 计算区间内 元素累计总和,并将其返回

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // value 起始值

    示例:

    1. //常用的算术生成算法 accumulate
    2. void test01() {
    3. vector<int> v;
    4. for (int i = 0; i <= 100;i++) {
    5. v.push_back(i);
    6. }
    7. int sum = accumulate(v.begin(),v.end(),0);
    8. cout << "vector容器元素之和:" << sum << endl;
    9. }
    10. int main() {
    11. test01();
    12. system("pause");
    13. return 0;
    14. }

    注意事项:accumulate使用时头文件注意是 numeric,这个算法很实用

    5.2、fill

    功能描述:

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

    函数原型:

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

    // beg 开始迭代器

    // end 结束迭代器

    // value 填充的值

    示例:

    1. //常用的算术生成算法 fill
    2. void myPrint(int val) {
    3. cout << val << " ";
    4. }
    5. void test01() {
    6. vector<int> v;
    7. v.resize(10);
    8. for_each(v.begin(),v.end(),myPrint);
    9. cout << endl;
    10. //重新填充为 100
    11. fill(v.begin(),v.end(),100);
    12. for_each(v.begin(), v.end(), myPrint);
    13. cout << endl;
    14. }
    15. int main() {
    16. test01();
    17. system("pause");
    18. return 0;
    19. }

    6、常用集合算法

    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. //常用的集合算法 set_intersection(交集)
    5. void myPrint(int val) {
    6. cout << val << " ";
    7. }
    8. void test01() {
    9. vector<int> v1;
    10. vector<int> v2;
    11. for (int i = 0; i < 10;i++) {
    12. v1.push_back(i+1);
    13. v2.push_back(i+6);
    14. }
    15. for_each(v1.begin(),v1.end(),myPrint);
    16. cout << endl;
    17. for_each(v2.begin(), v2.end(), myPrint);
    18. cout << endl;
    19. vector<int> vt;
    20. //需要分配空间,空间大小为两个集合的最小值
    21. vt.resize(min(v1.size(),v2.size()));
    22. //返回值是交集的最后一个元素迭代器
    23. vector<int>::iterator itEnd =
    24. set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),vt.begin());
    25. //1、下界迭代器为:v.end()
    26. for_each(vt.begin(),vt.end(),myPrint);
    27. cout << endl;
    28. //2、下界迭代器为:itEnd
    29. for_each(vt.begin(), itEnd, myPrint);
    30. cout << endl;
    31. }
    32. int main() {
    33. test01();
    34. system("pause");
    35. return 0;
    36. }

    注意事项:

    • 求交集的两个集合必须的有序序列
    • 目标容器开辟空间需要从两个容器中取小值
    • set_intersection返回值既是交集中最后一个元素的位置

    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. //常用的集合算法 set_union(并集)
    2. void myPrint(int val) {
    3. cout << val << " ";
    4. }
    5. void test01() {
    6. vector<int> v1;
    7. vector<int> v2;
    8. for (int i = 0; i < 10; i++) {
    9. v1.push_back(i + 1);
    10. v2.push_back(i + 6);
    11. }
    12. for_each(v1.begin(), v1.end(), myPrint);
    13. cout << endl;
    14. for_each(v2.begin(), v2.end(), myPrint);
    15. cout << endl;
    16. vector<int> vt;
    17. //需要分配空间,空间大小为两个集合大小之和
    18. vt.resize(v1.size()+v2.size());
    19. //返回值是并集的最后一个元素迭代器
    20. vector<int>::iterator itEnd =
    21. set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vt.begin());
    22. //1、下界迭代器为:v.end()
    23. for_each(vt.begin(), vt.end(), myPrint);
    24. cout << endl;
    25. //2、下界迭代器为:itEnd
    26. for_each(vt.begin(), itEnd, myPrint);
    27. cout << endl;
    28. }
    29. int main() {
    30. test01();
    31. system("pause");
    32. return 0;
    33. }

    注意事项:

    • 求并集的两个集合必须的有序序列
    • 目标容器开辟空间需要两个容器相加
    • set_union返回值是并集中最后一个元素的位置

    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. //常用的集合算法 set_difference(差集)
    2. void myPrint(int val) {
    3. cout << val << " ";
    4. }
    5. void test01() {
    6. vector<int> v1;
    7. vector<int> v2;
    8. for (int i = 0; i < 10; i++) {
    9. v1.push_back(i + 1);
    10. v2.push_back(i + 6);
    11. }
    12. for_each(v1.begin(), v1.end(), myPrint);
    13. cout << endl;
    14. for_each(v2.begin(), v2.end(), myPrint);
    15. cout << endl;
    16. vector<int> vt;
    17. //需要分配空间,空间大小为两个集合的最大值
    18. vt.resize(max(v1.size(),v2.size()));
    19. // 1、v1 和 v2 的差集
    20. //返回值是并集的最后一个元素迭代器
    21. vector<int>::iterator itEnd =
    22. set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vt.begin());
    23. cout << endl;
    24. //下界迭代器为:itEnd
    25. cout << "v1 和 v2 的差集:" << endl;
    26. for_each(vt.begin(), itEnd, myPrint);
    27. cout << endl;
    28. //1、 v2 和 v1 的差集
    29. itEnd =set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vt.begin());
    30. cout << endl;
    31. //下界迭代器为:itEnd
    32. cout << "v2 和 v1 的差集:" << endl;
    33. for_each(vt.begin(), itEnd, myPrint);
    34. cout << endl;
    35. }
    36. int main() {
    37. test01();
    38. system("pause");
    39. return 0;
    40. }

    注意事项:

    • 求差集的两个集合必须的有序序列
    • 目标容器开辟空间需要从两个容器取较大值
    • set_difference返回值是差集中最后一个元素的位置
  • 相关阅读:
    架构-三层架构:三层架构
    Codeforces Round #821(Div.2) (A-D2) 题解
    HTML我的家乡宁夏学生网页设计作品 dreamweaver作业静态HTML网页设计模板 宁夏旅游景点网页作业制作...
    22.括号生成
    java中的lambda表达式
    PHP:namespace 关键字和 __NAMESPACE__ 常量
    论文精读:Medical Transformer: Gated Axial-Attention forMedical Image Segmentation
    HiveServer2 报错 OutOfMemoryError 解决思路
    【Python】python多线程
    Blazor OIDC 单点登录授权实例5 - 独立SSR App (net8 webapp ) 端授权
  • 原文地址:https://blog.csdn.net/weixin_57439178/article/details/130801996