replace_copy_if
检查在源区的每个元素并替换它,如果其满足指定的谓词,则结果复制到新的目标范围时。
template<class InputIterator, class OutputIterator, class Predicate, class Type>
OutputIterator replace_copy_if(
InputIterator _First,
InputIterator _Last,
OutputIterator _Result,
Predicate _Pred,
const Type& _Val
);
参数
_First
指向第一的元素位置的输入迭代器。元素替换的范围。_Last
指向在最终元素的位置一的输入迭代器。元素替换的范围。_Result
指向第一元素的输出位置的迭代器。元素复制的目标范围。_Pred
必须满足的一元谓词是元素的值来替换。_Val
分配给的值满足谓词的元素的新值。
返回值
指向在最终元素的位置个的输出在迭代器元素修订的序列复制的目标范围为。
备注
引用的源和目标关联范围不能重叠并且必须都有效:所有指针必须 dereferenceable,然后在序列中的最后位置从开始来访问通过递增。
不替换的元素的顺序保持稳定。
operator== 用于确定在元素相等必须对在其操作数之间存在着用关系。
复杂线性;具有 (_Last - _First) 是否相等比较和负荷 (_Last - _First) 的新值赋。
replace_copy_if 有两个相关的窗体:
有关这些功能如何运行的信息,请参阅 经过检查的迭代器。
示例
// alg_replace_copy_if.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
bool greater6 ( int value ) {
return value >6;
}
int main( ) {
using namespace std;
vector <int> v1;
list <int> L1 (13);
vector <int>::iterator Iter1;
list <int>::iterator L_Iter1;
int i;
for ( i = 0 ; i <= 9 ; i++ )
v1.push_back( i );
int ii;
for ( ii = 0 ; ii <= 3 ; ii++ )
v1.push_back( 7 );
random_shuffle ( v1.begin( ), v1.end( ) );
int iii;
for ( iii = 0 ; iii <= 13 ; iii++ )
v1.push_back( 1 );
cout << "The original vector v1 is:\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Replace elements with a value of 7 in the 1st half of a vector
// with a value of 70 and copy it into the 2nd half of the vector
replace_copy_if ( v1.begin( ), v1.begin( ) + 14,v1.end( ) -14,
greater6 , 70);
cout << "The vector v1 with values of 70 replacing those greater"
<< "\n than 6 in the 1st half & copied into the 2nd half is:\n ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Replace elements in a vector with a value of 70
// with a value of 1 and copy into a list
replace_copy_if ( v1.begin( ), v1.begin( ) + 13,L1.begin( ),
greater6 , -1 );
cout << "A list copy of vector v1 with the value -1\n replacing "
<< "those greater than 6 is:\n ( " ;
for ( L_Iter1 = L1.begin( ) ; L_Iter1 != L1.end( ) ; L_Iter1++ )
cout << *L_Iter1 << " ";
cout << ")." << endl;
}
示例输出
The original vector v1 is:
( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ).
The vector v1 with values of 70 replacing those greater
than 6 in the 1st half & copied into the 2nd half is:
( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 70 1 70 2 0 70 70 3 4 6 70 5 70 70 ).
A list copy of vector v1 with the value -1
replacing those greater than 6 is:
( -1 1 -1 2 0 -1 -1 3 4 6 -1 5 -1 ).
要求
标头: <算法>
命名空间: std