Namespaces
Variants

std:: 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
partition_point
(C++11)

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
헤더 파일에 정의됨 <algorithm>
template < class ForwardIt, class UnaryPred >
ForwardIt partition_point ( ForwardIt first, ForwardIt last, UnaryPred p ) ;
(C++11부터)
(C++20부터 constexpr)

분할된 범위 [ first , last ) 를 검사하고 첫 번째 분할의 끝, 즉 p 를 만족하지 않는 첫 번째 요소를 찾거나, 모든 요소가 p 를 만족할 경우 last 를 반환합니다.

만약 elem 요소들이 [ first , last ) 범위 내에서 표현식 bool ( p ( elem ) ) 에 대해 분할되지 않은 경우 , 동작은 정의되지 않습니다.

목차

매개변수

first, last - 분할된 요소들을 검사할 범위를 정의하는 반복자 쌍 range
p - 범위의 시작 부분에서 발견된 요소들에 대해 ​ true 를 반환하는 단항 predicate

표현식 p ( v ) VT 타입의 (const 가능성 있는) 모든 인수 v 에 대해 bool 로 변환 가능해야 하며, value category 에 관계없이 v 를 수정해서는 안 됩니다. 따라서 VT & 매개변수 타입은 허용되지 않으며 , VT 에 대해 이동이 복사와 동등하지 않는 한 VT 타입도 허용되지 않습니다 (C++11부터) . ​

타입 요구사항
-
ForwardIt LegacyForwardIterator 요구사항을 충족해야 합니다.
-
UnaryPred Predicate 요구사항을 충족해야 합니다.

반환값

[ first , last ) 범위 내 첫 번째 파티션의 끝 바로 다음을 가리키는 반복자, 또는 모든 요소가 p 를 만족할 경우 last .

복잡도

주어진 N std:: distance ( first, last ) 인 경우, 술어 p O(log(N)) 회 적용합니다.

참고 사항

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

가능한 구현

template<class ForwardIt, class UnaryPred>
constexpr //< since C++20
ForwardIt partition_point(ForwardIt first, ForwardIt last, UnaryPred p)
{
    for (auto length = std::distance(first, last); 0 < length; )
    {
        auto half = length / 2;
        auto middle = std::next(first, half);
        if (p(*middle))
        {
            first = std::next(middle);
            length -= (half + 1);
        }
        else
            length = half;
    }
    return first;
}

예제

#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::partition(v.begin(), v.end(), is_even);
    print_seq("After partitioning, v: ", v.cbegin(), v.cend());
    const auto pp = std::partition_point(v.cbegin(), v.cend(), is_even);
    const auto i = std::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: 8 2 6 4 5 3 7 1 9
Partition point is at 4; v[4] = 5
First partition (all even elements): 8 2 6 4
Second partition (all odd elements): 5 3 7 1 9

참고 항목

특정 조건을 만족하는 첫 번째 요소를 찾음
(function template)
(C++11)
범위가 오름차순으로 정렬되었는지 확인
(function template)
주어진 값보다 작지 않은 첫 번째 요소에 대한 반복자를 반환
(function template)
분할된 범위의 분할 지점을 찾음
(algorithm function object)