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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值