STL 之replace,replace_if,replace_copy,replace_copy_if
声明:
- #include <algorithm>
- template <class forwardItr,class Type>
- void replace(forwardItr first, forwardItr last,const Type& oldValue const Type& newValue);
- template <class forwardItr, class unaryPredicate,class Type>
- void replace_if(forwardItr first, forwardItr last, unaryPredicate op,const Type& newValue);
- template <class inputItr,class outputItr,class Type>
- outputItr replace_copy(inputItr first1, inputItr last1, outputItr destFirst,const Type& oldValue, const Type& newValue);
- template <class inputItr,class outputItr, class unaryPredicate>
- outputItr replace_copy_if(inputItr first1, inputItr last1, outputItr destFirst, unaryPredicate op,const Type& newValue);
示例代码:
- #include <iostream>
- #include <list>
- #include <string>
- #include <numeric>
- #include <iterator>
- #include <vector>
- #include <functional>
- #include <algorithm>
- using namespace std;
- bool lessThanEqual50(int num) {
- return (num <= 50);
- }
- int main() {
- char cList[10] = {'A','a','A','B','A','c','D','e','F','A'};
- vector<char> charList(cList,cList+10);
- ostream_iterator<char> screen(cout, " ");
- cout << "charList:" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- // replace
- // 将容器中的A替换为Z
- replace(charList.begin(),charList.end(),'A','Z');
- cout << "charList.replace A -> Z:" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- // replace_if
- // 将所有的大写字母替换为*
- replace_if(charList.begin(),charList.end(),isupper,'*');
- cout << "charList.replace_if Upper->*" << endl;
- copy(charList.begin(),charList.end(),screen);
- cout << endl;
- int listi[10] = {12,34,56,21,34,78,34,55,12,25};
- vector<int> intList(listi,listi+10);
- ostream_iterator<int> screenInt(cout, " ");
- cout << "intList:" << endl;
- copy(intList.begin(),intList.end(),screenInt);
- cout << endl;
- vector<int> temp1(10);
- // 将intList中34全部替换为0,并输出到temp1中,不改变intList
- replace_copy(intList.begin(),intList.end(),temp1.begin(),34,0);
- cout << "intList.replace_copy:" << endl;
- copy(intList.begin(),intList.end(),screenInt);
- cout << endl;
- cout << "temp1:" << endl;
- copy(temp1.begin(),temp1.end(),screenInt);
- cout << endl;
- vector<int> temp2(10);
- // 将intList中小于50的全部替换为50,并输出到temp2中,不改变intList
- replace_copy_if(intList.begin(),intList.end(),temp2.begin(),lessThanEqual50,50);
- cout << "intList.replace_copy_if:" << endl;
- copy(intList.begin(),intList.end(),screenInt);
- cout << endl;
- cout << "temp2:" << endl;
- copy(temp2.begin(),temp2.end(),screenInt);
- cout << endl;
- return 0;
- }
运行结果:
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相关推荐
- C++ replace replace_if replace_copy replace_copy_if
#include <iostream> #include <list> #include <algorithm> #include <iterator> ...
- C++ STL 常用遍历算法
C++ STL 常用遍历算法 STL的容器算法迭代器的设计理念 1) STL的容器通过类模板技术,实现数据类型和容器模型的分离 2) STL的迭代器技术实现了遍历容器的统一方法:也为STL的算法提供 ...
- 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 ...
- c++高级编程学习笔记4
C++运算符重载 运算符重载概述 根据第 1 章的描述,C++中的运算符是一些类似于+.<.*和<<的符号.这些运算符可应用于内建类型,例如 int 和 double,从而实现算术操 ...
- 【c++算法】变动性算法
直接改变元素值,或者在复制到另一区间的过程中改变元素值 For_each 针对每个元素执行某项操作 Copy 从第一个元素开始复制某段区间 Copy_backward 从最后一个元素开始复制某段区间 ...
- STL源码剖析(三)
算法 从语言的角度看: 容器 Container 是一个class template 算法 Algorithm 是一个function template 迭代器 Iterator 是一个class t ...
- c++stl和std_std :: replace()函数以及C ++ STL中的示例
c++stl和std C ++ STL std :: replace()函数 (C++ STL std::replace() function) replace() function is a lib ...
- 最全ACM常用STL
STL 中专门用于排列的函数(可以处理存在重复数据集的排列问题) 头文件:#include <algorithm> using namespace std; 调用: next_permut ...
- ACM竞赛常用STL(二)之STL--algorithm
<algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的. 下面列举出<algorithm>中的模板函数: adjacent_find / binar ...
最新文章
- vs2015开发c语言 简书,微软符号服务器_NT_SYMBOL_PATH给VS调试带来的隐藏坑
- Google VC投资SDN初创公司Plexxi
- php 自定义打印模板下载,PHP – 创建自定义模板系统?
- python导入模块教程_Python 极简教程(二十四) - 导入模块
- 运放输入偏置电流方向_连载 | 运放参数的详细解释和分析part2如何测量输入偏置电流Ib和输入失调电流Ios...
- 排序算法:选择排序、插入排序、希尔排序
- 人类和编译器谁编写的代码更快?你们的答案也许都是错的
- mscaffe 训练minist数据
- CDMA-Rake接受技术
- 计算器html js php代码,html+js实现简单的计算器代码(加减乘除)
- 《三体3:死神永生》读后感
- 【技巧】屏蔽百度搜索热点和相关软件推荐等(提高注意力)
- 解读BLM业务领先模型中的业务设计
- 如何选择字体(font-family)
- Fedora Firefox flash-player插件安装
- 医学统计学 第四章(定量资料的统计描述)
- SEO快排系统功能更新
- 代理自动配置文件PAC的使用方法
- Python写违章扣分程序
- 43、DNS域名系统(应用层)