C ++: lla on luokka STL -algoritmikirjastossa, joka antaa meille helpon osioalgoritmien avulla tiettyjä sisäänrakennettuja toimintoja. Osiolla tarkoitetaan konttien elementtien jakamista tietystä tilasta riippuen.
Osiot -
1. Osasto (kerjäämisolosuhteet) :- Tätä toimintoa käytetään jakautuvat elementit -lla kuntoperuste mainittu väitteissään.
2. IS_PARTIONTED (BEC -END -ELÄMÄ) :- Tämä toiminto palauttaa boolean Totta, jos säiliö on jaettu muuten palauttaa vääriä.
// 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; }
Lähtö:
regex java
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7
Yllä olevassa koodi -osiofunktiossa osiot vektori riippuen siitä, onko elementti tasainen vai pariton jopa elementit omituista elementeistä missään erityisessä järjestyksessä.
3. Stable_Partition (Beg End -olosuhde) :- Tätä toimintoa käytetään jakautuvat elementit -lla kuntoperuste mainittu väitteissään siten, että elementtien suhteellinen järjestys säilyy. .
4. Osion_point (Beg End -olosuhde) :- Tämä toiminto Palauttaa iteraattorin, joka osoittaa osiokohtaan säiliöstä, ts. Ensimmäinen elementti osioidulla alueella [alkaa), jolle ehto ei ole totta. Säiliö tulisi jo jaotella tämän toiminnon toimimiseksi.
// 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; }
Lähtö:
The partitioned vector is : 2 6 8 1 5 7 The vector elements returning true for condition are : 2 6 8
Yllä olevassa koodissa tasainen ja parittomat elementit jaotellaan ja kasvavassa järjestyksessä (lajiteltu). Ei aina kasvavassa järjestyksessä, vaikka tässä elementit (tasainen ja pariton) ilmestyivät lisääntyneessä järjestyksessä, joten tulos osion jälkeen. Jos Vect olisi ollut {217865} Stable_Partitionin jälkeen (), se olisi {286175}. Ulkonäysjärjestys ylläpidetään.
5. osiot_copy (Beg End Beg1 Beg2 -tila) :- Tämä toiminto kopioi osioidut elementit sen väitteissä mainituissa eri säiliöissä. Se vie 5 argumenttia. Uuden säiliön alkamisaseman aloitus- ja päättymisasento, jossa elementit on kopioitava (elementit, jotka palaavat totta ehtoon) Uuden säiliön aloituspaikka, jossa muut elementit on kopioitava (elementit palauttavat vääriä ehtoja) ja ehto) ja ehto . Muuttamis- uudet astiat on välttämätöntä Tätä toimintoa varten.
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; }
Lähtö:
The elements that return true for condition are : 2 6 8 The elements that return false for condition are : 1 5 7