C++ 算法 partition() 函數用於根據其參數中提到的給定謂詞(條件)對元素進行分區。如果容器已分區,則此函數返回 true,否則返回 false。
用法
template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator partition (BidirectionalIterator first,
BidirectionalIterator last, UnaryPredicate pred); //until C++ 11
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator partition (ForwardIterator first,
ForwardIterator last, UnaryPredicate pred); //Since C++ 11
參數
first: 一個雙向迭代器,指向要分區的範圍內的第一個元素。
last:一個雙向迭代器,指向要分區的範圍中過去的最後一個元素。
pred: 用戶定義的一元謂詞函數,它定義要對元素進行分類時要滿足的條件。
返回值
此函數返回一個迭代器,指向不滿足謂詞條件的範圍的第一個元素。
複雜度
複雜度在 [first, last) 範圍內是線性的:將 pred 應用於每個元素,並可能交換其中的一些元素。
數據競爭
範圍 [first, last) 中的對象被修改。
異常
如果元素交換或迭代器上的操作引發異常,則此函數將引發異常。
請注意,無效參數會導致未定義的行為。
例子1
讓我們看一個簡單的例子來演示 partition() 的使用:
#include <iostream> // std::cout
#include <algorithm> // std::partition
#include <vector> // std::vector
using namespace std;
bool IsOdd (int i) { return (i%2)==1; }
int main () {
vector<int> myvector;
// set some values:
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
vector<int>::iterator bound;
bound = partition (myvector.begin(), myvector.end(), IsOdd);
// print out content:
cout << "odd elements:";
for (vector<int>::iterator it=myvector.begin(); it!=bound; ++it)
cout << ' ' << *it;
cout << '\n';
cout << "even elements:";
for (vector<int>::iterator it=bound; it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
輸出:
odd elements:1 9 3 7 5 even elements:6 4 8 2
在上麵的例子中,從 1 到 9 的元素被劃分為偶數和奇數元素。
例子2
讓我們看另一個簡單的例子:
#include <vector>
#include <algorithm>
#include <iostream>
bool greater5 ( int value ) {
return value >5;
}
int main( ) {
using namespace std;
vector <int> v1, v2;
vector <int>::iterator Iter1, Iter2;
int i;
for ( i = 0 ; i <= 10 ; i++ )
{
v1.push_back( i );
}
random_shuffle( v1.begin( ), v1.end( ) );
cout << "Vector v1 is ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
// Partition the range with predicate greater10
partition ( v1.begin( ), v1.end( ), greater5 );
cout << "The partitioned set of elements in v1 is:( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
return 0;
}
輸出:
Vector v1 is ( 4 10 7 8 0 5 2 1 6 9 3 ). The partitioned set of elements in v1 is:( 9 10 7 8 6 5 2 1 0 4 3 ).
例子3
讓我們看另一個使用 partition() 函數快速排序向量元素的簡單示例:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
#include <forward_list>
template <class ForwardIt>
void quicksort(ForwardIt first, ForwardIt last)
{
if(first == last) return;
auto pivot = *std::next(first, std::distance(first,last)/2);
ForwardIt middle1 = std::partition(first, last,
[pivot](const auto& em){ return em < pivot; });
ForwardIt middle2 = std::partition(middle1, last,
[pivot](const auto& em){ return !(pivot < em); });
quicksort(first, middle1);
quicksort(middle2, last);
}
int main()
{
std::vector<int> v = {0,1,2,3,4,5,6,7,8,9};
std::cout << "Original vector:\n ";
for(int elem:v) std::cout << elem << ' ';
auto it = std::partition(v.begin(), v.end(), [](int i){return i % 2 == 0;});
std::cout << "\nPartitioned vector:\n ";
std::copy(std::begin(v), it, std::ostream_iterator<int>(std::cout, " "));
std::cout << " * ";
std::copy(it, std::end(v), std::ostream_iterator<int>(std::cout, " "));
std::forward_list<int> fl = {1, 30, -4, 3, 5, -4, 1, 6, -8, 2, -5, 64, 1, 92};
std::cout << "\nUnsorted list:\n ";
for(int n:fl) std::cout << n << ' ';
std::cout << '\n';
quicksort(std::begin(fl), std::end(fl));
std::cout << "Sorted using quicksort:\n ";
for(int fi:fl) std::cout << fi << ' ';
std::cout << '\n';
return 0;
}
輸出:
Original vector: 0 1 2 3 4 5 6 7 8 9 Partitioned vector: 0 8 2 6 4 * 5 3 7 1 9 Unsorted list: 1 30 -4 3 5 -4 1 6 -8 2 -5 64 1 92 Sorted using quicksort: -8 -5 -4 -4 1 1 1 2 3 5 6 30 64 92
示例 4
讓我們看另一個簡單的例子:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = {1, 2, 3, 4, 5};
cout<<"Before Partition:";
for_each(v.begin(), v.end(), [](int v) {
cout << v << " ";
});
auto pred = [](int x) { return x % 2 == 0; };
// Divide it into an even group and an odd group
partition(v.begin(), v.end(), pred);
cout<<"\nAfter partition:";
for_each(v.begin(), v.end(), [](int x) {
cout << x << " ";
});
cout<<"\n\nIs it partitioned?"<<endl;
// Is it divided into an even group and an odd group?
if (is_partitioned(v.begin(), v.end(), pred)) {
cout << "Yes, it is partitioned" << endl;
}
else {
cout << "No, it is not partitioned" << endl;
}
return 0;
}
輸出:
Before Partition:1 2 3 4 5 After partition:4 2 3 1 5 Is it partitioned? Yes, it is partitioned
相關用法
- C++ Algorithm partition_copy()用法及代碼示例
- C++ Algorithm partition_point()用法及代碼示例
- C++ Algorithm partial_sort()用法及代碼示例
- C++ Algorithm partial_sort_copy()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm prev_permutation()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
- C++ Algorithm transform()用法及代碼示例
- C++ Algorithm set_difference()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm partition()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。