(8)STL算法之替换
在STL算法中可以使用替换函数replace()、replace_if()完成替换操作。先来看下replace函数的底层实现。
// replace() 声明与实现
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value);
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value)
{
while (first!=last)
{
if (*first == old_value)
*first=new_value;
++first;
}
}
再来看看replace_if()的底层实现...
template <class ForwardIterator, class UnaryPredicate, class T>
void replace_if (ForwardIterator first, ForwardIterator last,UnaryPredicate pred, const T& new_value );
template < class ForwardIterator, class UnaryPredicate, class T >
void replace_if (ForwardIterator first, ForwardIterator last,UnaryPredicate pred, const T& new_value)
{
while ( first != last )
{
if ( pred( *first ) )
*first = new_value;
++first;
}
}
越看越觉得C语言真牛逼
这两个函数都是有条件的替换,replace()是将容器[first, last]范围中与old_value相等的元素,替换成new_value;而replace_if()是将容器[first, last]范围中能使函数pred为真的元素,替换成new_value。下来看一个例子。
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> // bind2nd(less<int>(), 7);
using namespace std;
void main()
{
vector<int> v;
int arr[] = { 1,2,3,4,5,6,7,8,9 };
v.assign(arr, arr + 9);
for (int val : v)
cout << val << " "; // 1 2 3 4 5 6 7 8 9
cout << endl;
replace(v.begin(),v.end(),5,10); //将等于 5 的元素替换成 10
for (int val : v)
cout << val << " "; // 1 2 3 4 10 6 7 8 9
cout << endl;
replace_if(v.begin(), v.end(), bind2nd(less<int>(), 7), 11); //将小于 7 的元素换成 11
for (int val : v)
cout << val << " "; // 11 11 11 11 10 11 7 8 9
cout << endl;
}
===========================================================================================================================================
再加两个替换函数replace_copy()、replace_copy_if()
template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy (InputIterator first, InputIterator last,OutputIterator result,const T& old_value, const T& new_value);
template <class InputIterator, class OutputIterator, class T>
OutputIterator replace_copy (InputIterator first, InputIterator last,OutputIterator result,const T& old_value, const T& new_value)
{
while (first!=last)
{
*result = (*first==old_value)? new_value: *first;
++first;
++result;
}
return result;
}
template <class InputIterator, class OutputIterator, class UnaryPredicate, class T>
OutputIterator replace_copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred,const T& new_value);
template <class InputIterator, class OutputIterator, class UnaryPredicate, class T>
OutputIterator replace_copy_if (InputIterator first, InputIterator last,OutputIterator result, UnaryPredicate pred,const T& new_value)
{
while (first!=last)
{
*result = ( pred(*first)) ? new_value : *first;
++first;
++result;
}
return result;
}
用一个例子来学会它
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool fun(int elem)
{
return ((elem % 2 == 1));
}
int main() {
int arr[] = { 10, 11, 12, 13, 14, 15, 11, 11 };
vector<int> v1(8),v2;
replace_copy(arr, arr + 8, v1.begin(), 11, 21);
for (auto it = v1.begin(); it != v1.end(); ++it)
cout << *it <<" "; // 10 21 12 13 14 15 21 21
cout << endl;
v2.resize(v1.size());
replace_copy_if(v1.begin(), v1.end(), v2.begin(), fun, 0); //复制容器并将单数全部置 0
for (auto it = v2.begin(); it != v2.end(); ++it)
cout << *it << " "; // 10 0 12 0 14 0 0 0
cout << endl;
return 0;
}