C++ 算法 中的 partition_point函数

首页 / C++入门教程 / C++ 算法 中的 partition_point函数

C++算法 partition_point()函数用于返回给定参数内pred不正确的第一个元素。以满足条件的元素排在不满足条件的元素之前的方式对元素进行排序。

partition_point - 语法

template <class ForwardIterator, class UnaryPredicate>
ForwardIterator partition_point (ForwardIterator first, ForwardIterator last,
                                   UnaryPredicate pred);

partition_point - 参数

first:一个正向迭代器,指向参数中的第一个元素以检查条件。

last:指向参数的最后一个元素的前向迭代器。

pred :用户定义的一元谓词函数,用于定义要测试的条件。

partition_point - 返回值

此函数返回一个正向迭代器,以指向不满足由pred测试的条件的第一个元素;如果找不到,则返回最后一个。

partition_point - 例子1

让我们看一个简单的示例来演示partition_point()的用法:

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>

using namespace std;
 
int main()
{
    array<int, 9> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
    auto is_even = [](int i)
        { return i % 2 == 0; };
    partition(v.begin(), v.end(), is_even);
 
    auto p = std::partition_point(v.begin(), v.end(), is_even);
 
    cout << "Before partition:\n    ";
    copy(v.begin(), p, ostream_iterator<int>(cout, " "));
    cout << "\nAfter partition:\n    ";
    copy(p, v.end(), ostream_iterator<int>(cout, " "));
    
    return 0;
}

输出:

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

partition_point - 例子2

让我们看另一个简单的例子:

#include <iostream>    //std::cout
#include <algorithm>   //std::partition, std::partition_point
#include <vector>      //std::vector

using namespace std;

bool IsOdd (int i) { return (i%2)==1; }

int main () {
  vector<int> foo {1,2,3,4,5,6,7,8,9};
  vector<int> odd;

  partition (foo.begin(),foo.end(),IsOdd);

  auto it = partition_point(foo.begin(),foo.end(),IsOdd);
  odd.assign (foo.begin(),it);

 //print contents of odd:
  cout << "odd:";
  for (int& x:odd) cout << ' ' << x;
  cout << '\n';

  return 0;
}

输出:

odd: 1 9 3 7 5

partition_point - 例子3

让我们看另一个简单的例子:

#include<iostream> 
#include<algorithm>//for partition algorithm 
#include<vector>//for vector 
using namespace std; 
int main() 
{ 
   //Initializing vector 
    vector<int> vect = { 2, 1, 5, 6, 8, 7 }; 
      
   //partitioning vector using stable_partition() 
   //in sorted order 
    stable_partition(vect.begin(), vect.end(), [](int x) 
    { 
        return x%2 == 0;         
    }); 
      
   //Displaying partitioned Vector 
    cout << "The partitioned vector is : "; 
    for (int &x : vect) cout << x << " "; 
    cout << endl; 
      
   //Declaring iterator 
    vector<int>::iterator it1; 
      
   //using partition_point() to get ending position of partition 
    auto it = partition_point(vect.begin(), vect.end(), [](int x) 
    { 
        return x%2==0; 
    }); 
      
   //Displaying partitioned Vector 
    cout << "The vector elements returning true for condition are : "; 
    for ( it1= vect.begin(); it1!=it; it1++) 
    cout << *it1 << " "; 
    cout << endl; 
      
    return 0; 
      
} 

输出:

The partitioned vector is : 2 6 8 1 5 7 
The vector elements returning true for condition are : 2 6 8 

partition_point - 例子4

让我们看另一个简单的例子:

#include <iostream>//std::cout 
#include <algorithm>//std::partition, std::partition_point 
#include <vector>//std::vector 

using namespace std;
  
bool IsNegative(int i) { return (i < 0); } 
  
int main() 
{ 
    vector<int> data{ 1, -1, 3, -4, 5, 2, -2, 4, -5, -3 }; 
    vector<int> negative, positive; 
  
   //partition data on the basis of odd elements using  
   //pred IsNegative() 
    stable_partition(data.begin(), data.end(), IsNegative); 
  
   //gets the partition point based on odd elements 
    auto it = 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: 
    cout << "Negative: "; 
    for (int& x : negative) 
        cout << ' ' << x; 
    cout << '\n'; 
  
   //print contents of even: 
    cout << "Positive: "; 
    for (int& x : positive) 
        cout << ' ' << x; 
    cout << '\n'; 
  
    return 0; 
}

输出:

Negative:  -1 -4 -2 -5 -3
Positive:  1 3 5 2 4

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

即时消息技术剖析与实战 -〔袁武林〕

架构实战案例解析 -〔王庆友〕

视觉笔记入门课 -〔高伟〕

手机摄影 -〔@随你们去〕

Go 语言项目开发实战 -〔孔令飞〕

Tony Bai · Go语言第一课 -〔Tony Bai〕

自动化测试高手课 -〔柳胜〕

快速上手C++数据结构与算法 -〔王健伟〕

结构执行力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)