partition_point()获取分区点:返回一个迭代到所述第一元件在分区范围[第一,最后一个)满足pred(谓词)是不正确的,这表明它的分区点。
该范围内的元素应已经分区,就像使用相同的参数调用了partition一样。
该函数通过比较排序范围内的非连续元素来优化比较次数,这对于随机访问迭代器特别有效。
句法 :
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.
该函数模板的定义等效于:
ForwardIterator partition_point(ForwardIterator first,
ForwardIterator last,
UnaryPredicate pred);
范例1:
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;
}
输出:
// C++ program to get odd elements
// and even elements
#include // std::cout
#include // std::partition, std::partition_point
#include // std::vector
bool IsOdd(int i) { return (i % 2) == 1; }
int main()
{
std::vector data{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector odd, even;
// partition data on the basis of odd elements
// using pred IsOdd()
std::stable_partition(data.begin(), data.end(), IsOdd);
// gets the partition point based on odd elements
auto it = std::partition_point(data.begin(), data.end(),
IsOdd);
// assign elements to odd from beginning till partition
// point.
odd.assign(data.begin(), it);
even.assign(it, data.end());
// print contents of odd:
std::cout << "odd:";
for (int& x : odd)
std::cout << ' ' << x;
std::cout << '\n';
// print contents of even:
std::cout << "even:";
for (int& x : even)
std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
范例2:
odd: 1 3 5 7 9
even: 2 4 6 8 10
输出:
// C++ program to get negative elements
// and positive elements using partition_point
#include // std::cout
#include // std::partition, std::partition_point
#include // std::vector
bool IsNegative(int i) { return (i < 0); }
int main()
{
std::vector data{ 1, -1, 3, -4, 5, 2, -2, 4,
-5, -3 };
std::vector negative, positive;
// partition data on the basis of odd elements using
// pred IsNegative()
std::stable_partition(data.begin(), data.end(),
IsNegative);
// gets the partition point based on odd elements
auto it = std::partition_point(data.begin(), data.end(),
IsNegative);
// assign elements to odd from beginning till
// partition point.
negative.assign(data.begin(), it);
positive.assign(it, data.end());
// print contents of odd:
std::cout << "Negative: ";
for (int& x : negative)
std::cout << ' ' << x;
std::cout << '\n';
// print contents of even:
std::cout << "Positive: ";
for (int& x : positive)
std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
复杂度:
执行大约log2(N)+2个元素比较(其中N是该距离)。在非随机访问迭代器上,迭代器进阶自身会产生平均N的额外线性复杂度。