Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/partition point"

From cppreference.com
< cpp‎ | algorithm
(Changed complexity to use math)
(number of iterator increments is O(N) --- copy wording from std::lower_bound, and wikilink it)
Line 24: Line 24:
  
 
===Complexity===
 
===Complexity===
{{math|O(log(N))}}, where {{c|1=N = std::distance(first, last)}}, applications of the predicate p.
+
Given {{c|1=N = std::distance(first, last)}}, performs {{math|O(log N)}} applications of the predicate {{tt|p}}.
  
 +
However, for non-{{lc|RandomAccessIterator}}s, the number of iterator increments is {{math|O(N)}}.
  
 
===Example===
 
===Example===
Line 60: Line 61:
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc inc | cpp/algorithm/dsc is_sorted}}
 
{{dsc inc | cpp/algorithm/dsc is_sorted}}
 +
{{dsc inc | cpp/algorithm/dsc lower_bound}}
 
{{dsc end}}
 
{{dsc end}}
  

Revision as of 19:38, 23 April 2017

 
 
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
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
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Defined in header <algorithm>
template< class ForwardIt, class UnaryPredicate >
ForwardIt partition_point( ForwardIt first, ForwardIt last, UnaryPredicate p );
(1) (since C++11)

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

Contents

Parameters

first, last - the partitioned range of elements to examine
p - unary predicate which returns ​true for the elements found in the beginning of the range.

The expression p(v) must be convertible to bool for every argument v of type (possibly const) VT, where VT is the value type of ForwardIt, regardless of value category, and must not modify v. Thus, a parameter type of VT&is not allowed, nor is VT unless for VT a move is equivalent to a copy(since C++11). ​

Type requirements

Template:par req concept Template:par req concept

Return value

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

Complexity

Given N = std::distance(first, last), performs O(log N) applications of the predicate p.

However, for non-RandomAccessIterators, the number of iterator increments is O(N).

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 };
 
    auto is_even = [](int i){ return i % 2 == 0; };
    std::partition(v.begin(), v.end(), is_even);
 
    auto p = std::partition_point(v.begin(), v.end(), is_even);
 
    std::cout << "Before partition:\n    ";
    std::copy(v.begin(), p, std::ostream_iterator<int>(std::cout, " "));
    std::cout << "\nAfter partition:\n    ";
    std::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

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