Namespaces
Variants

std::ranges:: partition_point

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:: forward_iterator I, std:: sentinel_for < I > S,

class Proj = std:: identity ,
std:: indirect_unary_predicate < std :: projected < I, Proj >> Pred >
constexpr I

partition_point ( I first, S last, Pred pred, Proj proj = { } ) ;
(1) (C++20 이후)
template < ranges:: forward_range R,

class Proj = std:: identity ,
std:: indirect_unary_predicate <
std :: projected < ranges:: iterator_t < R > , Proj >> Pred >
constexpr ranges:: borrowed_iterator_t < R >

partition_point ( R && r, Pred pred, Proj proj = { } ) ;
(2) (C++20 이후)

분할된(마치 ranges::partition 에 의해 처리된 것처럼) 범위 [ first , last ) 또는 r 를 검사하고 첫 번째 파티션의 끝, 즉 pred 를 만족하지 않는 투영된 요소 또는 모든 투영된 요소가 pred 를 만족하는 경우 last 를 찾습니다.

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

목차

매개변수

first, last - 부분적으로 정렬된 범위 를 정의하는 반복자-감시자 쌍
r - 검사할 부분적으로 정렬된 범위
pred - 투영된 요소들에 적용할 조건자
proj - 요소들에 적용할 투영

반환값

[ first , last ) 범위 내 첫 번째 파티션의 끝 바로 다음 반복자, 또는 모든 투영된 요소가 pred 를 만족하는 경우 last 와 동일한 반복자.

복잡도

주어진 N = ranges:: distance ( first, last ) 에 대해, 술어 pred 와 투영 proj O(log N) 회 적용을 수행합니다.

그러나 센티넬이 std:: sized_sentinel_for < I > 를 모델링하지 않는 경우, 반복자 증감 횟수는 O(N) 입니다.

참고 사항

이 알고리즘은 ranges::lower_bound 의 보다 일반화된 형태로, ranges::partition_point 와 다음 조건자를 사용하여 표현할 수 있습니다: [ & ] ( auto const & e ) { return std:: invoke ( pred, e, value ) ; } ) ; .

예제

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
auto print_seq = [](auto rem, auto first, auto last)
{
    for (std::cout << rem; first != last; std::cout << *first++ << ' ') {}
    std::cout << '\n';
};
int main()
{
    std::array v {1, 2, 3, 4, 5, 6, 7, 8, 9};
    auto is_even = [](int i) { return i % 2 == 0; };
    std::ranges::partition(v, is_even);
    print_seq("After partitioning, v: ", v.cbegin(), v.cend());
    const auto pp = std::ranges::partition_point(v, is_even);
    const auto i = std::ranges::distance(v.cbegin(), pp);
    std::cout << "Partition point is at " << i << "; v[" << i << "] = " << *pp << '\n';
    print_seq("First partition (all even elements): ", v.cbegin(), pp);
    print_seq("Second partition (all odd elements): ", pp, v.cend());
}

가능한 출력:

After partitioning, v: 2 4 6 8 5 3 7 1 9
Partition point is at 4; v[4] = 5
First partition (all even elements): 2 4 6 8
Second partition (all odd elements): 5 3 7 1 9

참고 항목

범위가 오름차순으로 정렬되었는지 확인합니다
(알고리즘 함수 객체)
주어진 값보다 작지 않은 첫 번째 요소에 대한 반복자를 반환합니다
(알고리즘 함수 객체)
분할된 범위의 분할 지점을 찾습니다
(함수 템플릿)