partition_point in C++
Last Updated :
05 Aug, 2018
partition_point() Gets the partition point : Returns an iterator to the first element in the partitioned range [first, last) for which pred(predicate) is not true, indicating its partition point.
The elements in the range shall already be partitioned, as if partition had been called with the same arguments.
The function optimizes the number of comparisons performed by comparing non-consecutive elements of the sorted range, which is specially efficient for random-access iterators.
Syntax :
ForwardIterator partition_point(ForwardIterator first,
ForwardIterator last,
UnaryPredicate pred);
Parameters :
first, last :
Forward iterators to the initial and final positions of the partitioned sequence. The range checked is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
pred
Unary function that accepts an element in the range as argument, and returns a value convertible to bool. The value returned indicates whether the element goes before the partition point (if true, it goes before; if false goes at or after it). The function shall not modify its argument. This can either be a function pointer or a function object.
Return Value :
An iterator to the first element in the partitioned range [first, last) for which pred is not true, or last if it is not true for any element.
The definition of this function template is equivalent to:
template
ForwardIterator partition_point (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred)
{
auto n = distance(first, last);
while (n>0)
{
ForwardIterator it = first;
auto step = n/2;
std::advance (it, step);
if (pred(*it)) { first=++it; n-=step+1; }
else n=step;
}
return first;
}
Example 1:
#include <iostream> // std::cout
#include <algorithm> // std::partition, std::partition_point
#include <vector> // std::vector
bool IsOdd( int i) { return (i % 2) == 1; }
int main()
{
std::vector< int > data{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector< int > odd, even;
std::stable_partition(data.begin(), data.end(), IsOdd);
auto it = std::partition_point(data.begin(), data.end(),
IsOdd);
odd.assign(data.begin(), it);
even.assign(it, data.end());
std::cout << "odd:" ;
for ( int & x : odd)
std::cout << ' ' << x;
std::cout << '\n' ;
std::cout << "even:" ;
for ( int & x : even)
std::cout << ' ' << x;
std::cout << '\n' ;
return 0;
}
|
Output:
odd: 1 3 5 7 9
even: 2 4 6 8 10
Example 2:
#include <iostream> // std::cout
#include <algorithm> // std::partition, std::partition_point
#include <vector> // std::vector
bool IsNegative( int i) { return (i < 0); }
int main()
{
std::vector< int > data{ 1, -1, 3, -4, 5, 2, -2, 4,
-5, -3 };
std::vector< int > negative, positive;
std::stable_partition(data.begin(), data.end(),
IsNegative);
auto it = std::partition_point(data.begin(), data.end(),
IsNegative);
negative.assign(data.begin(), it);
positive.assign(it, data.end());
std::cout << "Negative: " ;
for ( int & x : negative)
std::cout << ' ' << x;
std::cout << '\n' ;
std::cout << "Positive: " ;
for ( int & x : positive)
std::cout << ' ' << x;
std::cout << '\n' ;
return 0;
}
|
Output:
Negative: -1 -4 -2 -5 -3
Positive: 1 3 5 2 4
Complexity :
Performs approximately log2(N)+2 element comparisons (where N is this distance). On non-random-access iterators, the iterator advances produce themselves an additional linear complexity in N on average.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...