C++ STL常用算法-65-拷贝和替换算法

港控/mmm° 2022-11-30 04:02 310阅读 0赞

继续学习C++中常用的算法基本使用,这篇学习拷贝和替换算法,一共有四个,用来操作容器中指定范围的元素的拷贝和替换操作。

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

1.copy 拷贝算法

函数原型:copy(iterator beg, iterator end, iterator dst);

参数解释:

第一个参数容器1的迭代器开始位置,参数2 容器1的迭代器结束位置,参数3 目标容器的迭代器的开始位置

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. void Print01(int val)
  7. {
  8. cout << val << " ";
  9. };
  10. void test01()
  11. {
  12. vector<int> v1;
  13. v1.push_back(10);
  14. v1.push_back(50);
  15. v1.push_back(30);
  16. v1.push_back(40);
  17. v1.push_back(20);
  18. // copy 拷贝算法
  19. vector<int> v2;
  20. v2.resize(v1.size());
  21. copy(v1.begin(), v1.end(), v2.begin());
  22. //打印v2容器内容
  23. for_each(v2.begin(), v2.end(), Print01);
  24. cout << endl;
  25. }
  26. int main()
  27. {
  28. test01();
  29. system("pause");
  30. return 0;
  31. }

运行结果:

2020082316065886.png

2.replace 替换算法

替换算法就是在指定范围内把旧元素替换成新元素,例如在一个数列中,把数字5替换成55.

函数原型:replace(iterator beg, iterator end, oldValue, newValue);

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. void Print01(int val)
  7. {
  8. cout << val << " ";
  9. };
  10. void test01()
  11. {
  12. vector<int> v1;
  13. v1.push_back(10);
  14. v1.push_back(5);
  15. v1.push_back(30);
  16. v1.push_back(40);
  17. v1.push_back(5);
  18. // replace 替换算法
  19. replace(v1.begin(), v1.end(), 5, 55);
  20. //打印v2容器内容
  21. for_each(v1.begin(), v1.end(), Print01);
  22. cout << endl;
  23. }
  24. int main()
  25. {
  26. test01();
  27. system("pause");
  28. return 0;
  29. }

测试结果:

20200823161130543.png

注意,如果填写要替换的值数列中找不到,改替换函数是不起作用的。

3.replace_if 条件替换算法

函数原型:replace_if(iterator beg, iterator end, _Pred, newValue);

第三个函数是一个谓词,也就是返回类型是bool的仿函数,普通函数这里不行

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. class MyClass
  7. {
  8. public:
  9. bool operator()(int val)
  10. {
  11. return val < 30;
  12. }
  13. };
  14. void Print01(int val)
  15. {
  16. cout << val << " ";
  17. };
  18. void test01()
  19. {
  20. vector<int> v1;
  21. v1.push_back(10);
  22. v1.push_back(15);
  23. v1.push_back(30);
  24. v1.push_back(40);
  25. v1.push_back(5);
  26. // replace_if 替换算法, 把小于30的元素都替换成200
  27. replace_if(v1.begin(), v1.end(), MyClass(), 200);
  28. //打印v2容器内容
  29. for_each(v1.begin(), v1.end(), Print01);
  30. cout << endl;
  31. }
  32. int main()
  33. {
  34. test01();
  35. system("pause");
  36. return 0;
  37. }

代码运行结果:

20200823161833698.png

4.swap 交换算法

这个前面也学习过,作用就是互换两个容器的元素

函数原型:swap(容器1,容器2);

注意事项:

1.两个容器必须是同类型

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. class MyClass
  7. {
  8. public:
  9. bool operator()(int val)
  10. {
  11. return val < 30;
  12. }
  13. };
  14. void Print01(int val)
  15. {
  16. cout << val << " ";
  17. };
  18. void test01()
  19. {
  20. vector<int> v1;
  21. vector<int> v2;
  22. for(int i=0; i < 10; i++)
  23. {
  24. v1.push_back(i);
  25. v2.push_back(i+100);
  26. }
  27. cout << "before merge。。" << endl;
  28. for_each(v1.begin(), v1.end(), Print01);
  29. cout << endl;
  30. for_each(v2.begin(), v2.end(), Print01);
  31. cout << endl;
  32. cout << "==============================" << endl;
  33. // swap算法,合并两个容器
  34. swap(v1, v2);
  35. for_each(v1.begin(), v1.end(), Print01);
  36. cout << endl;
  37. for_each(v2.begin(), v2.end(), Print01);
  38. cout << endl;
  39. }
  40. int main()
  41. {
  42. test01();
  43. system("pause");
  44. return 0;
  45. }

运行结果:

20200823172759752.png

发表评论

表情:
评论列表 (有 0 条评论,310人围观)

还没有评论,来说两句吧...

相关阅读