---
title: "STL 之replace,replace_if,replace_copy,replace_copy_if"
author: "mywebcode"
date: "2014-04-15T13:47:00Z"
tags: []
slug: "stl-replace-replace-if-replace-copy-replace-copy-if"
draft: false
lastmod: "2014-04-30T09:14:06Z"
id: 2058268
url: "https://mywebcode.iteye.com/blog/2058268"
---

[返回](http://blog.csdn.net/haifengzhilian/article/details/23687175)

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

声明：

```
#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 -&gt; Z:  
Z a Z B Z c D e F Z  
charList.replace\_if Upper-&gt;*  
\* 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