function template
<algorithm>
std::partition_pointtemplate <class ForwardIterator, class UnaryPredicate> ForwardIterator partition_point (ForwardIterator first, ForwardIterator last, UnaryPredicate pred);
Get partition point
Returns an iterator to the first element in the partitioned range[first,last)
for which pred 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.
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class ForwardIterator, class UnaryPredicate>
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;
}
[first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
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).
[first,last)
for which pred is not true
, or last if it is not true
for any element.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// partition_point example
#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> foo {1,2,3,4,5,6,7,8,9};
std::vector<int> odd;
std::partition (foo.begin(),foo.end(),IsOdd);
auto it = std::partition_point(foo.begin(),foo.end(),IsOdd);
odd.assign (foo.begin(),it);
// print contents of odd:
std::cout << "odd:";
for (int& x:odd) std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
log2(N)+2
element comparisons (where N is this distance).
[first,last)
are accessed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4