C++在其STL算法庫中有一個類,該類允許我們使用某些內置函數輕鬆進行分區算法。分區是指根據給定條件劃分容器元素的動作。
分區操作 :
1. partition(beg,end,condition):-此函數用於根據其參數中提到的條件對元素進行分區。
2. is_partitioned(beg,end,condition):-如果容器已分區,則此函數返回布爾值true,否則返回false。
// C++ code to demonstrate the working of
// partition() and is_partitioned()
#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 };
// Checking if vector is partitioned
// using is_partitioned()
is_partitioned(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
})?
cout << "Vector is partitioned":
cout << "Vector is not partitioned";
cout << endl;
// partitioning vector using partition()
partition(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
});
// Checking if vector is partitioned
// using is_partitioned()
is_partitioned(vect.begin(), vect.end(), [](int x)
{
return x%2==0;
})?
cout << "Now, vector is partitioned after partition operation":
cout << "Vector is still not partitioned after partition operation";
cout << endl;
// Displaying partitioned Vector
cout << "The partitioned vector is:";
for (int &x:vect) cout << x << " ";
return 0;
}
輸出:
Vector is not partitioned Now, vector is partitioned after partition operation The partitioned vector is:2 8 6 5 1 7
在上麵的代碼中,分區函數根據元素是偶數還是奇數對向量進行分區,偶數元素從奇數元素開始的劃分沒有特定順序。
3. stable_partition(beg,end,condition):-此函數用於根據其自變量中提到的條件對元素進行分區,從而保留元素的相對順序。
4. partition_point(beg,end,condition):-此函數返回一個迭代器,該迭代器指向容器的分區點,即條件不成立的分區範圍[beg,end)中的第一個元素。容器應該已經分區了,此函數才能起作用。
// C++ code to demonstrate the working of
// stable_partition() and partition_point()
#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
在上麵的代碼中,偶數和奇數元素按升序進行了劃分(排序)
5. partition_copy(beg,end,beg1,beg2, condition):-此函數在其參數中提到的differnet容器中複製分區的元素。它需要5個參數。容器的開始和結束位置,必須複製元素的新容器的開始位置(元素返回條件為true),必須複製其他元素的新容器的開始位置(元素返回條件為false)和條件。調整新容器的大小對於此函數是必需的。
// C++ code to demonstrate the working of
// partition_copy()
#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 };
// Declaring vector1
vector<int> vect1;
// Declaring vector1
vector<int> vect2;
// Resizing vectors to suitable size using count_if() and resize()
int n = count_if (vect.begin(), vect.end(), [](int x)
{
return x%2==0;
} );
vect1.resize(n);
vect2.resize(vect.size()-n);
// Using partition_copy() to copy partitions
partition_copy(vect.begin(), vect.end(), vect1.begin(),
vect2.begin(), [](int x)
{
return x%2==0;
});
// Displaying partitioned Vector
cout << "The elements that return true for condition are:";
for (int &x:vect1)
cout << x << " ";
cout << endl;
// Displaying partitioned Vector
cout << "The elements that return false for condition are:";
for (int &x:vect2)
cout << x << " ";
cout << endl;
return 0;
}
輸出:
The elements that return true for condition are:2 6 8 The elements that return false for condition are:1 5 7
相關用法
注:本文由純淨天空篩選整理自 std::partition in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。