algorithm_remove_copy

algorithm_remove_copy (제거하며 복사)

std::remove_copy는 소스 범위에서 기준을 만족하는 요소를 제외하고 나머지 요소를 목적지 범위로 복사해요. 원본을 변경하지 않아요.

출처: cppreference

본문

std::remove_copy는 소스 범위 [first, last)에서 특정 기준을 만족하는 요소를 무시하고 나머지 요소를 d_first에서 시작하는 목적지 범위로 복사해요. <algorithm> 헤더에 정의되어 있어요.

template< class InputIt, class OutputIt, class T >
OutputIt remove_copy( InputIt first, InputIt last,
                      OutputIt d_first, const T& value );

template< class InputIt, class OutputIt, class UnaryPred >
OutputIt remove_copy_if( InputIt first, InputIt last,
                         OutputIt d_first, UnaryPred p );
  • remove_copyoperator==value와 같은 모든 요소를 무시하고 복사해요.
  • remove_copy_if — 술어 p가 참을 반환하는 모든 요소를 무시하고 복사해요.

반환값 (Return value)

복사된 마지막 요소 다음의 반복자예요.

복잡도 (Complexity)

Nstd::distance(first, last)라고 하면 정확히 N번의 p 적용(또는 비교)이 필요해요.

예제 (Example)

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::vector<int> v{1, 2, 3, 4, 5, 3, 7};
    std::vector<int> out;
    std::remove_copy(v.begin(), v.end(), std::back_inserter(out), 3);
    for (int x : out) std::cout << x << ' ';
    std::cout << '\n';
}

출력:

1 2 4 5 7

더 알아보기 (Learn more)

cppreference