C++ verfügt über eine Klasse in seiner STL-Algorithmenbibliothek, die uns einfache Partitionierungsalgorithmen mithilfe bestimmter integrierter Funktionen ermöglicht. Unter Unterteilung versteht man den Vorgang der Aufteilung von Behälterelementen abhängig von einer bestimmten Bedingung.
Partitionsoperationen :
1. Partition (Beg-End-Bedingung) :- Diese Funktion dient dazu Partitionieren Sie die Elemente An Grundlage der Bedingung in seinen Argumenten erwähnt.
2. is_partitioned(Beg-End-Bedingung) :- Diese Funktion gibt einen booleschen Wert zurück true, wenn der Container partitioniert ist Sonst gibt false zurück.
// 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; }
Ausgabe:
sonniges Deol-Zeitalter
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7
Im obigen Code partitioniert die Partitionsfunktion den Vektor abhängig davon, ob ein Element gerade oder ungerade ist. Gerade Elemente werden in keiner bestimmten Reihenfolge von ungeraden Elementen partitioniert.
3. stabile_partition(Beg-End-Bedingung) :- Diese Funktion dient dazu Partitionieren Sie die Elemente An Grundlage der Bedingung in seinen Argumenten erwähnt so dass die relative Reihenfolge der Elemente erhalten bleibt. .
4. partition_point(Beg-End-Bedingung) :- Diese Funktion gibt einen Iterator zurück, der auf den Partitionspunkt zeigt des Containers, d. h. das erste Element im partitionierten Bereich [begend], für das die Bedingung nicht wahr ist. Damit diese Funktion funktioniert, sollte der Container bereits partitioniert sein.
// 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; }
Ausgabe:
The partitioned vector is : 2 6 8 1 5 7 The vector elements returning true for condition are : 2 6 8
Im obigen Code werden gerade und ungerade Elemente partitioniert und in aufsteigender Reihenfolge sortiert (sortiert). Nicht immer in aufsteigender Reihenfolge, allerdings erscheinen hier die Elemente (gerade und ungerade) in erhöhter Reihenfolge, ebenso das Ergebnis nach der Partitionierung. Wenn vect nach Stable_partition() { 217865 } gewesen wäre, wäre es { 286175 }. Die Reihenfolge des Erscheinens wird beibehalten.
5. partition_copy(beg end beg1 beg2 Bedingung) :- Diese Funktion kopiert die partitionierten Elemente in den verschiedenen in seinen Argumenten genannten Containern. Es sind 5 Argumente erforderlich. Anfangs- und Endposition des Containers, Anfangsposition des neuen Containers, in den Elemente kopiert werden müssen (Elemente, die für die Bedingung „true“ zurückgeben), Anfangsposition des neuen Containers, an die andere Elemente kopiert werden müssen (Elemente, die für die Bedingung „false“ zurückgeben) und die Bedingung . Größenänderung neue Container ist notwendig für diese Funktion.
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; }
Ausgabe:
The elements that return true for condition are : 2 6 8 The elements that return false for condition are : 1 5 7