C ++ má ve své knihovně algoritmů STL třídu, která nám umožňuje snadné algoritmy rozdělení pomocí určitých vestavěných funkcí. Oddíl se týká akt dělení prvků kontejnerů v závislosti na daném stavu.
Operace oddílu :
1. Oddíl (podmínka BEG END) :- Tato funkce se používá Rozdíl prvků na základ podmínky zmíněno ve svých argumentech.
2. IS_PARTITORED (BEG END CONDES) :- Tato funkce vrací Boolean Pravda, pokud je kontejner rozdělen jinak vrací FALSE.
// C++ code to demonstrate the working of // partition() and is_partitioned() #include #include // for partition algorithm #include // 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; }
Výstup:
reverzní řetězec java
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7
Ve výše uvedeném oddílu oddílu kódové oddíly vektoru vektoru v závislosti na tom, zda je prvek rovnoměrný nebo lichý prvky, jsou rozděleny z lichých prvků v žádném konkrétním pořadí.
3. stabilní_partice (koncová podmínka BEG) :- Tato funkce se používá Rozdíl prvků na základ podmínky uvedeno ve svých argumentech v Takový způsob, jak je zachován relativní řád prvků. .
4. districe_point (BEG END CONDED) :- tato funkce Vrátí iterátor směřující do bodu oddílu kontejneru, tj. První prvek v rozsahu oddílu [Begend), pro který není podmínka pravdivá. Kontejner by měl být již rozdělen na to, aby tato funkce fungovala.
// C++ code to demonstrate the working of // stable_partition() and partition_point() #include #include // for partition algorithm #include // 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; }
Výstup:
The partitioned vector is : 2 6 8 1 5 7 The vector elements returning true for condition are : 2 6 8
Ve výše uvedeném kódu jsou rozděleny a liché prvky a v rostoucím pořadí (tříděné). Ne vždy v rostoucím pořadí, i když se zde prvky (sudé a liché) objevily ve zvýšeném pořadí, je to výsledek po oddílu. Pokud by byl Vect {217865} po stabilní_partici (), bylo by to {286175}. Pořadí vzhledu je udržováno.
5. divertition_copy (BEG END BEG1 BEG2 PODMÍNKA) :- tato funkce Kopíruje rozdělené prvky v různých kontejnerech uvedených v jeho argumentech. Trvá 5 argumentů. Počáteční a koncová poloha počáteční polohy nádoby na novém kontejneru, kde je třeba zkopírovat prvky (prvky vracející se pro podmínku) Počáteční poloha nového kontejneru, kde je třeba zkopírovat jiné prvky (prvky, které se pro podmínky vrací nepravdivé . Změna velikosti nové kontejnery je nutné Pro tuto funkci.
CPP
// C++ code to demonstrate the working of // partition_copy() #include #include // for partition algorithm #include // 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; }
Výstup:
The elements that return true for condition are : 2 6 8 The elements that return false for condition are : 1 5 7