Namespaces
Variants
Views
Actions

std::ranges::partition_point

From cppreference.com
< cpp‎ | algorithm‎ | ranges
Revision as of 08:26, 17 June 2020 by Cjdb (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 
 
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
(C++11)                (C++11)(C++11)

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
(C++11)
(C++17)
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
partition_point
  
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Fold operations
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 
Defined in header <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) (since C++20)
template< std::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) (since C++20)

Examines the partitioned (as if by ranges::partition) range [first, last) and locates the end of the first partition, that is, the projected element that does not satisfy pred or last if all projected elements satisfy pred.

Contents

Parameters

first, last - iterator-sentinel defining the partially-ordered range to examine
r - the partially-ordered range to examine
value - value to compare the elements to
pred - predicate to apply to the projected elements
proj - projection to apply to the elements

Return value

The iterator past the end of the first partition within [first, last) or last if all projected elements satisfy pred.

Complexity

Given N = ranges::distance(first, last), performs O(log N) applications of the predicate pred and projection proj.

However, sentinels that don't model std::sized_sentinel_for<I>, the number of iterator increments is O(N).

Notes

This algorithm is a more general form of ranges::lower_bound, which can be expressed in terms of ranges::partition_point with the predicate [&](auto const& e) { return std::invoke(pred, e, value); });.

Example

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
 
int main()
{
    std::array<int, 9> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
    namespace ranges = std::ranges;
    auto is_even = [](int i){ return i % 2 == 0; };
    ranges::partition(v.begin(), v.end(), is_even);
 
    auto p = ranges::partition_point(v, is_even);
 
    std::cout << "Before partition:\n    ";
    ranges::copy(v.begin(), p, std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\nAfter partition:\n    ";
    ranges::copy(p, v.end(), std::ostream_iterator<int>(std::cout, " "));
}

Output:

Before partition:
    8 2 6 4 
After partition:
    5 3 7 1 9

See also

checks whether a range is sorted into ascending order
(niebloid)[edit]
returns an iterator to the first element not less than the given value
(niebloid)[edit]