Namespaces
Variants

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

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
헤더 파일에 정의됨 <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 ) 의 요소들을 술어(predicate) pred 가 반환하는 값에 따라 두 개의 서로 다른 출력 범위로 복사합니다. proj 에 의한 투영(projection) 후 술어 pred 를 만족하는 요소들은 out_true 에서 시작하는 범위로 복사됩니다. 나머지 요소들은 out_false 에서 시작하는 범위로 복사됩니다. 입력 범위가 출력 범위 중 하나와 겹치는 경우 동작은 정의되지 않습니다.
2) (1) 과 동일하지만, r 을 소스 범위로 사용하며, 마치 ranges:: begin ( r ) first 로, ranges:: end ( r ) last 로 사용하는 것과 같습니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:

목차

매개변수

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

참고 항목

원소들의 범위를 두 그룹으로 분할합니다
(알고리즘 함수 객체)
원소들의 상대적 순서를 유지하면서 두 그룹으로 분할합니다
(알고리즘 함수 객체)
원소들의 범위를 새로운 위치로 복사합니다
(알고리즘 함수 객체)
특정 조건을 만족하는 원소들을 제외하고 범위를 복사합니다
(알고리즘 함수 객체)
원소들을 두 그룹으로 분할하면서 복사합니다
(함수 템플릿)