Namespaces
Variants

std::ranges::partition_copy, std::ranges::partition_copy_result

cppreference.net에서
 
 
알고리즘 라이브러리
제약된 알고리즘 및 범위에 대한 알고리즘 (C++20)
제약된 알고리즘, 예: ranges::copy, ranges::sort, ...
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 탐색 연산
(분할된 범위에 대해)
집합 연산 (정렬된 범위에 대해)
병합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
제약된 알고리즘
이 메뉴의 모든 이름은 네임스페이스에 속합니다 std::ranges
수정하지 않는 시퀀스 연산
수정하는 시퀀스 연산
분할 연산
정렬 연산
이진 탐색 연산 (정렬된 범위에 대해)
       
       
집합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
       
       
순열 연산
폴드 연산
수치 연산
(C++23)            
초기화되지 않은 저장소에 대한 연산
반환 타입
 
헤더에 정의됨 <algorithm>
호출 시그니처
template< std::input_iterator I, std::sentinel_for<I> S,
          std::weakly_incrementable O1, std::weakly_incrementable O2,
          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<I, Proj>> Pred >
requires std::indirectly_copyable<I, O1> &&
         std::indirectly_copyable<I, O2>
constexpr partition_copy_result<I, O1, O2>
    partition_copy( I first, S last, O1 out_true, O2 out_false,
                    Pred pred, Proj proj = {} );
(1) (C++20부터)
template< ranges::input_range R,
          std::weakly_incrementable O1, std::weakly_incrementable O2,
          class Proj = std::identity,
          std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred >
requires std::indirectly_copyable<ranges::iterator_t<R>, O1> &&
         std::indirectly_copyable<ranges::iterator_t<R>, O2>
constexpr partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2>
    partition_copy( R&& r, O1 out_true, O2 out_false,
                    Pred pred, Proj proj = {} );
(2) (C++20부터)
헬퍼 타입
template< class I, class O1, class O2 >
using partition_copy_result = ranges::in_out_out_result<I, O1, O2>;
(3) (C++20부터)
1) 입력 범위 [first, last)의 요소들을 조건자 pred가 반환하는 값에 따라 두 개의 다른 출력 범위로 복사합니다. 조건자 pred를 투영 proj으로 적용했을 때 만족하는 요소들은 out_true에서 시작하는 범위로 복사됩니다. 나머지 요소들은 out_false에서 시작하는 범위로 복사됩니다. 입력 범위가 출력 범위 중 하나와 겹치면 동작이 정의되지 않습니다.
2) (1)과 동일하지만, r을 소스 범위로 사용하며, ranges::begin(r)을 first로, ranges::end(r)을 last로 사용하는 것과 같습니다.

이 페이지에 설명된 함수형 개체는 알고리즘 함수 객체 (비공식적으로 niebloid라고 함)이며, 즉:

  • 명시적 템플릿 인자 목록은 이들 중 어떤 것을 호출할 때도 지정할 수 없습니다.
  • 이들 중 어떤 것도 인자 종속 조회에 보이지 않습니다.
  • 이들 중 어떤 것이 일반 비한정 조회에 의해 함수 호출 연산자의 왼쪽에 있는 이름으로 발견되면, 인자 종속 조회가 억제됩니다.

매개변수

first, last - 복사할 원소들의 소스 범위 를 정의하는 반복자-센티널 쌍
r - 복사할 원소들의 소스 범위
out_true - pred 를 만족하는 원소들을 위한 출력 범위의 시작
out_false - pred 를 만족하지 않는 원소들을 위한 출력 범위의 시작
pred - 투영된 원소들에 적용할 조건자
proj - 원소들에 적용할 투영

반환값

{ last, o1, o2 } , 여기서 o1 과 o2 는 복사가 완료된 후 각각 출력 범위의 끝을 나타냅니다.

복잡도

정확히 ranges:: distance ( first, last ) 번의 해당 predicate comp 및 projection proj 적용.

가능한 구현

struct partition_copy_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S,
             std::weakly_incrementable O1, std::weakly_incrementable O2,
             class Proj = std::identity, std::indirect_unary_predicate<
             std::projected<I, Proj>> Pred>
    requires std::indirectly_copyable<I, O1> && std::indirectly_copyable<I, O2>
    constexpr ranges::partition_copy_result<I, O1, O2>
        operator()(I first, S last, O1 out_true, O2 out_false,
                   Pred pred, Proj proj = {}) const
    {
        for (; first != last; ++first)
            if (!!std::invoke(pred, std::invoke(proj, *first)))
                *out_true = *first, ++out_true;
            else
                *out_false = *first, ++out_false;
        return {std::move(first), std::move(out_true), std::move(out_false)};
    }
    template<ranges::input_range R,
             std::weakly_incrementable O1, std::weakly_incrementable O2,
             class Proj = std::identity,
             std::indirect_unary_predicate<std::projected<iterator_t<R>, Proj>> Pred>
    requires std::indirectly_copyable<ranges::iterator_t<R>, O1> &&
             std::indirectly_copyable<ranges::iterator_t<R>, O2>
    constexpr ranges::partition_copy_result<ranges::borrowed_iterator_t<R>, O1, O2>
        operator()(R&& r, O1 out_true, O2 out_false, Pred pred, Proj proj = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(out_true),
                       std::move(out_false), std::move(pred), std::move(proj));
    }
};
inline constexpr partition_copy_fn partition_copy {};

예제

#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
    const auto in = {'N', '3', 'U', 'M', '1', 'B', '4', 'E', '1', '5', 'R', '9'};
    std::vector<int> o1(size(in)), o2(size(in));
    auto pred = [](char c) { return std::isalpha(c); };
    auto ret = std::ranges::partition_copy(in, o1.begin(), o2.begin(), pred);
    std::ostream_iterator<char> cout {std::cout, " "};
    std::cout << "in = ";
    std::ranges::copy(in, cout);
    std::cout << "\no1 = ";
    std::copy(o1.begin(), ret.out1, cout);
    std::cout << "\no2 = ";
    std::copy(o2.begin(), ret.out2, cout);
    std::cout << '\n';
}

출력:

in = N 3 U M 1 B 4 E 1 5 R 9
o1 = N U M B E R
o2 = 3 1 4 1 5 9

참고 항목

요소 범위를 두 그룹으로 나눕니다
(알고리즘 함수 객체)
각 그룹 내 상대적 순서를 유지하면서 요소를 두 그룹으로 나눕니다
(알고리즘 함수 객체)
요소 범위를 새 위치로 복사합니다
(알고리즘 함수 객체)
특정 조건을 만족하는 요소를 생략하고 요소 범위를 복사합니다
(알고리즘 함수 객체)
요소를 두 그룹으로 나누어 범위를 복사합니다
(함수 템플릿)