作用:用一个新值替换指定区间内所有的指定元素。

声明:

  1. #include <algorithm>
  2. template <class forwardItr,class Type>
  3. void replace(forwardItr first, forwardItr last,const Type& oldValue const Type& newValue);
  4. template <class forwardItr, class unaryPredicate,class Type>
  5. void replace_if(forwardItr first, forwardItr last, unaryPredicate op,const Type& newValue);
  6. template <class inputItr,class outputItr,class Type>
  7. outputItr replace_copy(inputItr first1, inputItr last1, outputItr destFirst,const Type& oldValue, const Type& newValue);
  8. template <class inputItr,class outputItr, class unaryPredicate>
  9. outputItr replace_copy_if(inputItr first1, inputItr last1, outputItr destFirst, unaryPredicate op,const Type& newValue);

示例代码:

  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4. #include <numeric>
  5. #include <iterator>
  6. #include <vector>
  7. #include <functional>
  8. #include <algorithm>
  9. using namespace std;
  10. bool lessThanEqual50(int num) {
  11. return (num <= 50);
  12. }
  13. int main() {
  14. char cList[10] = {'A','a','A','B','A','c','D','e','F','A'};
  15. vector<char> charList(cList,cList+10);
  16. ostream_iterator<char> screen(cout, " ");
  17. cout << "charList:" << endl;
  18. copy(charList.begin(),charList.end(),screen);
  19. cout << endl;
  20. // replace
  21. // 将容器中的A替换为Z
  22. replace(charList.begin(),charList.end(),'A','Z');
  23. cout << "charList.replace A -> Z:" << endl;
  24. copy(charList.begin(),charList.end(),screen);
  25. cout << endl;
  26. // replace_if
  27. // 将所有的大写字母替换为*
  28. replace_if(charList.begin(),charList.end(),isupper,'*');
  29. cout << "charList.replace_if Upper->*" << endl;
  30. copy(charList.begin(),charList.end(),screen);
  31. cout << endl;
  32. int listi[10] = {12,34,56,21,34,78,34,55,12,25};
  33. vector<int> intList(listi,listi+10);
  34. ostream_iterator<int> screenInt(cout, " ");
  35. cout << "intList:" << endl;
  36. copy(intList.begin(),intList.end(),screenInt);
  37. cout << endl;
  38. vector<int> temp1(10);
  39. // 将intList中34全部替换为0,并输出到temp1中,不改变intList
  40. replace_copy(intList.begin(),intList.end(),temp1.begin(),34,0);
  41. cout << "intList.replace_copy:" << endl;
  42. copy(intList.begin(),intList.end(),screenInt);
  43. cout << endl;
  44. cout << "temp1:" << endl;
  45. copy(temp1.begin(),temp1.end(),screenInt);
  46. cout << endl;
  47. vector<int> temp2(10);
  48. // 将intList中小于50的全部替换为50,并输出到temp2中,不改变intList
  49. replace_copy_if(intList.begin(),intList.end(),temp2.begin(),lessThanEqual50,50);
  50. cout << "intList.replace_copy_if:" << endl;
  51. copy(intList.begin(),intList.end(),screenInt);
  52. cout << endl;
  53. cout << "temp2:" << endl;
  54. copy(temp2.begin(),temp2.end(),screenInt);
  55. cout << endl;
  56. return 0;
  57. }

运行结果:

charList:
A a A B A c D e F A
charList.replace A -> Z:
Z a Z B Z c D e F Z
charList.replace_if Upper->*
* a * * * c * e * *
intList:
12 34 56 21 34 78 34 55 12 25
intList.replace_copy:
12 34 56 21 34 78 34 55 12 25
temp1:
12 0 56 21 0 78 0 55 12 25
intList.replace_copy_if:
12 34 56 21 34 78 34 55 12 25
temp2:
50 50 56 50 50 78 50 55 50 50

STL 之replace,replace_if,replace_copy,replace_copy_if相关推荐

  1. C++ replace replace_if replace_copy replace_copy_if

    #include <iostream> #include <list> #include <algorithm> #include <iterator> ...

  2. C++ STL 常用遍历算法

    C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离  2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供 ...

  3. An introduction of STL for beginners

    Ok, guys, here is one more beginner's tutorial. This time its STL. If you are new to STL and interes ...

  4. c++高级编程学习笔记4

    C++运算符重载 运算符重载概述 根据第 1 章的描述,C++中的运算符是一些类似于+.<.*和<<的符号.这些运算符可应用于内建类型,例如 int 和 double,从而实现算术操 ...

  5. 【c++算法】变动性算法

    直接改变元素值,或者在复制到另一区间的过程中改变元素值 For_each 针对每个元素执行某项操作 Copy 从第一个元素开始复制某段区间 Copy_backward 从最后一个元素开始复制某段区间 ...

  6. STL源码剖析(三)

    算法 从语言的角度看: 容器 Container 是一个class template 算法 Algorithm 是一个function template 迭代器 Iterator 是一个class t ...

  7. c++stl和std_std :: replace()函数以及C ++ STL中的示例

    c++stl和std C ++ STL std :: replace()函数 (C++ STL std::replace() function) replace() function is a lib ...

  8. 最全ACM常用STL

    STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespace std; 调用: next_permut ...

  9. ACM竞赛常用STL(二)之STL--algorithm

    <algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的. 下面列举出<algorithm>中的模板函数: adjacent_find / binar ...

最新文章

  1. vs2015开发c语言 简书,微软符号服务器_NT_SYMBOL_PATH给VS调试带来的隐藏坑
  2. Google VC投资SDN初创公司Plexxi
  3. php 自定义打印模板下载,PHP – 创建自定义模板系统?
  4. python导入模块教程_Python 极简教程(二十四) - 导入模块
  5. 运放输入偏置电流方向_连载 | 运放参数的详细解释和分析part2如何测量输入偏置电流Ib和输入失调电流Ios...
  6. 排序算法:选择排序、插入排序、希尔排序
  7. 人类和编译器谁编写的代码更快?你们的答案也许都是错的
  8. mscaffe 训练minist数据
  9. CDMA-Rake接受技术
  10. 计算器html js php代码,html+js实现简单的计算器代码(加减乘除)
  11. 《三体3:死神永生》读后感
  12. 【技巧】屏蔽百度搜索热点和相关软件推荐等(提高注意力)
  13. 解读BLM业务领先模型中的业务设计
  14. 如何选择字体(font-family)
  15. Fedora Firefox flash-player插件安装
  16. 医学统计学 第四章(定量资料的统计描述)
  17. SEO快排系统功能更新
  18. 代理自动配置文件PAC的使用方法
  19. Python写违章扣分程序
  20. 43、DNS域名系统(应用层)

热门文章

  1. Spring Boot定时任务-Quartz基本使用
  2. 借用构造函数 组合继承 拷贝继承 总结继承
  3. mysql 字符大对象_第02期:MySQL 数据类型的艺术 - 大对象字段
  4. Spring AbstractAutowireCapableBeanFactory
  5. hamcrest详细介绍
  6. EFCore笔记之异步查询
  7. Socket-Client通信
  8. EL函数以及自定义标签的应用
  9. Smarty的assign定义变量
  10. CSS+DIV-CSS滤镜的应用