A C ++ rendelkezik egy osztályban az STL algoritmusok könyvtárában, amely lehetővé teszi a könnyű partíciós algoritmusokat bizonyos beépített funkciók segítségével. A partíció az adott feltételektől függően a konténerek elemeinek elválasztására utal.
Partíciós műveletek :
1. Partíció (Beg end feltétel) :- Ez a funkció megszokta Ossza meg az elemeket -on állapot alapja az érveiben megemlített.
2. :- Ez a funkció visszatér a logikai igaz, ha a tartály particionált egyébként hamis.
// 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; }
Kimenet:
latex szövegméret
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7
A fenti kód partíciós funkció partíciójában a vektor attól függően, hogy egy elem egyenletes -e, vagy akár páratlan, akár páratlan elemeket külön -külön elkülönítenek -e a páratlan elemekből, különös sorrendben.
3. Stable_partition (Beg end feltétel) :- Ez a funkció megszokta Ossza meg az elemeket -on állapot alapja az érveiben megemlített oly módon, hogy az elemek relatív sorrendje megmaradjon. -
4. Partition_point (Beg end feltétel) :- Ez a funkció Visszaad egy iterátort, aki a partíciós pontra mutat a konténer, azaz a particionált tartomány első eleme, amelyre a feltétel nem igaz. A tartályt már meg kell osztani, hogy ez a funkció működjön.
// 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; }
Kimenet:
The partitioned vector is : 2 6 8 1 5 7 The vector elements returning true for condition are : 2 6 8
A fenti kódban egyenletes és páratlan elemek vannak particionálva és a növekvő sorrendben (rendezve). Nem mindig egyre növekvő sorrendben, bár itt az elemek (egyenletes és páratlan) megnövekedett sorrendben jelentek meg, így a partíció utáni eredmény. Ha a vect {217865} lett volna a stabil_partition () után, akkor a {286175} lenne. A megjelenés sorrendje fennmarad.
5. Partition_copy (Beg End Beg1 Beg2 állapot) :- Ez a funkció másolja a particionált elemeket Az érveiben említett különféle konténerekben. 5 érvet vesz igénybe. A konténer kezdeti és befejező helyzete az új tartály kezdeti helyzete, ahol az elemeket másolni kell (az elemek állapotba visszatérő elemek) Az új tartály kezdeti helyzete, ahol más elemeket másolni kell (a feltételhez hamis elemek) és az állapot) és a feltétel) - Átméretezés új konténerek szükséges ehhez a funkcióhoz.
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; }
Kimenet:
The elements that return true for condition are : 2 6 8 The elements that return false for condition are : 1 5 7