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
    指向第一個項目位置的輸入 Iterator 在項目被取代的範圍。

  • _Last
    指向超過最後一個項目的位置上的輸入 Iterator 在項目被取代的範圍。

  • _Result
    指向修改項目的順序複製到其中的目標範圍中第一個項目的輸出 Iterator 的地方。

  • _OldVal
    被取代之項目的舊值。

  • _NewVal
    指定要與舊值之項目的新值。

傳回值

指向超過最後一個項目的位置上的輸出 Iterator 在項目已修改的順序複製到其中的目標範圍。

備註

參考的來源和目的範圍不可以重疊和都必須是有效的:任何指標必須 dereferenceable,並在序列中最後一個位置開始可取得的會增加。

不會被取代之項目的順序趨於穩定。

operator== 用來判斷在項目之間的相等必須強制在其運算元之間的一個層級的關聯性。

複雜的線性:有 (_Last – _First) 相等的比較和最多_Last (–) _First指派的新值。

replace_copy 有兩個關聯的表單:

如需這些函式如何運作的詳細資訊,請參閱 檢查過的 Iterator

範例

// 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)

標準樣板程式庫