< cpp‎ | algorithm
Revision as of 18:18, 22 January 2013 by Nate (Talk | contribs)


Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <algorithm>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class ForwardIt, class UnaryPredicate >
ForwardIt partition_point( ForwardIt first, ForwardIt last, UnaryPredicate p);

<td > (1) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

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 last if all elements satisfy p.



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 signature of the predicate function should be equivalent to the following:

 bool pred(const Type &a);

The signature does not need to have const &, but the function must not modify the objects passed to it.
The type Type must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type. ​

Type requirements
ForwardIt must meet the requirements of ForwardIterator.
UnaryPredicate must meet the requirements of Predicate.

Return value

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


Logarithmic in the distance between first and last


#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, " "));


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

See also

Template:cpp/algorithm/dcl list is sorted