logo

std :: particija u C ++ STL

C ++ ima klasu u svojoj knjižnici STL algoritma koja nam omogućuje jednostavne algoritme o particiji pomoću određenih ugrađenih funkcija. Podjela se odnosi na čin dijeljenja elemenata spremnika, ovisno o određenom stanju. 
Operacije podjele ::
1. Particija (uvjet za kraj) :- Ova je funkcija navikla podijeliti elemente na osnova stanja spomenuto u svojim argumentima.
2. IS_PARTITION (BEG END STvijet) :- Ova se funkcija vraća Boolean Istina ako je kontejner podijeljen inače vraća laž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;   } 

Izlaz: 



java inicijalizirati niz
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7

U gornjim particijama s particijom koda vektor je ovisno o tome je li element ujednačen ili neobičan, čak su i elementi podijeljeni od neparnih elemenata bez određenog redoslijeda. 
3. stabilna_particija (uvjet za kraj) :- Ova je funkcija navikla podijeliti elemente na osnova stanja spomenute u svojim argumentima u Takav način da se sačuva relativni redoslijed elemenata. .
4. particija_point (BEG krajnje uvjet) :- ova funkcija Vraća iterator ukazujući na točku particije kontejnera, tj. Prvi element u particioniranom rasponu [Begend) za koji uvjet nije istinit. Spremnik bi već trebao biti podijeljen da bi ova funkcija radila.

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

Izlaz: 

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

U gornjem kodu čak su i neobični elementi podijeljeni i u sve većem redoslijedu (sortirani). Ne uvijek u povećanju redoslijeda, iako su se ovdje elementi (čak i neobični) pojavili povećanim redoslijedom, pa je rezultat nakon podjele. Da bi Vect bio {217865} nakon stabilne_particije (), to bi bio {286175}. Održava se redoslijed izgleda.
5. particija_copy (BEG END BEG1 BEG2 Stanje) :- ova funkcija Kopira podijeljene elemente U različitim spremnicima spomenutim u njegovim argumentima. Potrebno je 5 argumenata. Početak i završetak položaja spremnika Početni položaj novog spremnika gdje se elementi moraju kopirati (elementi koji se vraćaju istinito za stanje) Početni položaj novog spremnika gdje se moraju kopirati drugi elementi (elementi koji se vraćaju za stanje) i uvjet . Promjenom veličine Novi kontejneri Je li neophodno za ovu funkciju.



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

Izlaz: 

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