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

요구 사항

헤더: <algorithm>

네임 스페이스: std

참고 항목

참조

replace_copy_if (STL Samples)

표준 템플릿 라이브러리