📜  C++中的partition_point

📅  最后修改于: 2021-05-30 13:36:12             🧑  作者: Mango

partition_point()获取分区点:返回一个迭代到所述第一元件在分区范围[第一,最后一个)满足pred(谓词)是不正确的,这表明它的分区点。

该范围内的元素应已经分区,就像使用相同的参数调用了partition一样。

该函数通过比较排序范围内的非连续元素来优化比较次数,这对于随机访问迭代器特别有效。

句法 :

该函数模板的定义等效于:

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的额外线性复杂度。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”