replace_copy

检查在源范围的每个元素并替换,则其与指定值,在复制结果到新的目标范围。

template<class InputIterator, class OutputIterator, class Type>
   OutputIterator replace_copy(
      InputIterator _First, 
      InputIterator _Last, 
      OutputIterator _Result,
      const Type& _OldVal, 
      const Type& _NewVal
   );

参数

  • _First
    指向第一个元素的位置的输入迭代器在元素中替换的范围。

  • _Last
    指向通过最终元素的位置一的输入迭代器在元素中替换的范围。

  • _Result
    指向在元素修改后的顺序进行复制的目标范围的第一个元素的输出迭代器为。

  • _OldVal
    替换元素的旧值。

  • _NewVal
    分配给使用旧值的元素的新值。

返回值

指向通过最终元素的位置个的输出器遍历元素修改后的顺序进行复制的目标范围为。

备注

引用的源和目标范围不能重叠并且必须都是有效的:所有指针必须dereferenceable,并在序列中最后位置以访问按增量。

未替换元素的顺序保持不变。

用于的 operator== 确定在元素相等必须实施在其操作数之间的等效性关系。

复杂的线性:具有(_Last – _First)相等性比较和最多_Last (–) _First新的值的赋值。

replace_copy 有两个相关的窗体:

有关这些功能如何的信息的行为,请参见 经过检查的迭代器

示例

// alg_replace_copy.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>

int main( ) {
   using namespace std;
   vector <int> v1;
   list <int> L1 (15);
   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 <= 15 ; 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 in one part of a vector with a value of 7
   // with a value of 70 and copy into another part of the vector
   replace_copy ( v1.begin( ), v1.begin( ) + 14,v1.end( ) -15, 7 , 70);

   cout << "The vector v1 with a value 70 replacing that of 7 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 ( v1.begin( ), v1.begin( ) + 14,L1.begin( ), 7 , 1);

   cout << "The list copy L1 of v1 with the value 0 replacing "
        << "that of 7 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 1 1 ).
The vector v1 with a value 70 replacing that of 7 is:
 ( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 1 70 1 9 2 0 70 70 3 4 6 8 5 70 70 1 ).
The list copy L1 of v1 with the value 0 replacing that of 7 is:
 ( 1 1 9 2 0 1 1 3 4 6 8 5 1 1 0 ).

要求

标头: <algorithm>

命名空间: std

请参见

参考

replace_copy (STL Samples)

标准模板库