logo

std :: particija v C ++ STL

C ++ ima razred v svoji knjižnici algoritmov STL, ki nam omogoča enostavno particijsko algoritme z uporabo nekaterih vgrajenih funkcij. Delitev se nanaša na dejanje delitve elementov zabojnikov, odvisno od določenega pogoja. 
Delitvene operacije :
1. particija (stanje konca) :- Ta funkcija se uporablja za delitve elementov naprej osnova stanja omenjena v svojih argumentih.
2. :- Ta funkcija vrne Boolean res, če je vsebnik razdeljen sicer se vrne napačno.

CPP
// 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;   } 

Izhod: 



podniz niza
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7

V zgornji funkciji particije kode delitve vektor, odvisno od tega, ali je element enakomeren ali neparen, celo elementi so razdeljeni iz nenavadnih elementov v določenem vrstnem redu. 
3. Stabilen_particija (stanje beg konca) :- Ta funkcija se uporablja za delitve elementov naprej osnova stanja omenjeno v svojih argumentih v tak način, da se ohrani relativni vrstni red elementov. .
4. particija_point (stanje konca) :- ta funkcija vrne iterator, ki kaže na točko particije vsebnika, tj. Prvi element v particioniranem območju [bere), za katerega stanje ni res. Vsebnik je treba že razdeliti, da bo ta funkcija delovala.

CPP
// 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;   } 

Izhod: 

The partitioned vector is : 2 6 8 1 5 7 The vector elements returning true for condition are : 2 6 8

V zgornji kodi so enakomerni in neparni elementi razdeljeni in v naraščajočem vrstnem redu (razvrščeni). Ne vedno v naraščajočem vrstnem redu, čeprav so se tukaj pojavili elementi (tudi in čudni) v povečanem vrstnem redu, zato je rezultat po delitvi. Če bi bil Vect {217865} po stabilu_partation (), bi bil to {286175}. Vrstni red videza se vzdržuje.
5. particija_copy (BEG END BEG1 BEG2 Stanje) :- ta funkcija Kopira razdeljene elemente v različnih zabojnikih, omenjenih v njegovih argumentih. Potrebno je 5 argumentov. Začetek in končni položaj zabojnika Začetni položaj novega vsebnika, kjer je treba kopirati elemente (elementi, ki se vračajo za pogoj) Začetni položaj novega vsebnika, kjer je treba kopirati druge elemente (elementi, ki se vračajo napačno), in pogoj . Spreminjanja Novi zabojniki je potrebno za to funkcijo.



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;  } 

Izhod: 

The elements that return true for condition are : 2 6 8 The elements that return false for condition are : 1 5 7