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 を特定します。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には 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

関連項目

範囲が昇順にソートされているかどうかをチェックする
(アルゴリズム関数オブジェクト)
指定された値より 小さくない 最初の要素へのイテレータを返す
(アルゴリズム関数オブジェクト)
分割された範囲の分割点を特定する
(関数テンプレート)