C++算法 partition_point()函数用于返回给定参数内pred不正确的第一个元素。以满足条件的元素排在不满足条件的元素之前的方式对元素进行排序。
template <class ForwardIterator, class UnaryPredicate> ForwardIterator partition_point (ForwardIterator first, ForwardIterator last, UnaryPredicate pred);
first:一个正向迭代器,指向参数中的第一个元素以检查条件。
last:指向参数的最后一个元素的前向迭代器。
pred :用户定义的一元谓词函数,用于定义要测试的条件。
此函数返回一个正向迭代器,以指向不满足由pred测试的条件的第一个元素;如果找不到,则返回最后一个。
让我们看一个简单的示例来演示partition_point()的用法:
链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-partition-point-function.html
来源:LearnFk无涯教程网
#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
让我们看另一个简单的例子:
#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
让我们看另一个简单的例子:
#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
让我们看另一个简单的例子:
#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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)