次の方法で共有


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
    要素が置き換えられている範囲の最後の要素の一つ前の位置 1 を示す入力反復子。

  • _Result
    要素がコピー先の範囲の先頭の要素の位置を指す出力反復子。

  • _Pred
    満たされている必要がある単項の述語は要素の値に置換されるようにしてください。

  • _Val
    古い値が述語を満たす要素に割り当てられた新しい値。

戻り値

を指す出力反復子要素の変更後のシーケンスがコピー先の範囲の最後の要素の一つ前の位置に 1。

解説

参照される元とコピー先の範囲を指定し、両方が有効である必要があります: すべてのポインターが dereferenceable なり、シーケンス内で最後の位置は incrementation によって最初からアクセスできます。

置き換えられなかった要素の順序の安定性維持されます。

要素間の等価性を判断するために使用される operator== がオペランド間の等価関係を課さなければ必要があります。

複雑度は、線形;。(_Last – _First) の等価性比較が最大で_Last  (– _First) 新しい値の割り当てです。

replace_copy_if 2 に関連する二つの形式があります:

ついては、これらの関数がどのようにするか、チェックを行う反復子を参照してください。

使用例

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

必要条件

ヘッダー: <algorithm>

名前空間: std

参照

関連項目

replace_copy_if (STL Samples)

標準テンプレート ライブラリ