logo

std :: partition en C++ STL

C++ a une classe dans sa bibliothèque d'algorithmes STL qui nous permet des algorithmes de partitionnement simples utilisant certaines fonctions intégrées. La partition fait référence à l'acte de diviser les éléments des conteneurs en fonction d'une condition donnée. 
Opérations de partitionnement :
1. partition (début de la condition de fin) :- Cette fonction est utilisée pour partitionner les éléments sur base de condition mentionné dans ses arguments.
2. is_partitioned (condition de fin de début) :- Cette fonction renvoie un booléen vrai si le conteneur est partitionné sinon renvoie faux.

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

Sortir: 



commander au hasard dans SQL
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7

Dans le code ci-dessus, la fonction de partition partitionne le vecteur selon qu'un élément est pair ou impair, les éléments pairs sont partitionnés des éléments impairs sans ordre particulier. 
3. stable_partition (condition de fin de début) :- Cette fonction est utilisée pour partitionner les éléments sur base de condition mentionné dans ses arguments dans de telle manière que l'ordre relatif des éléments soit préservé. .
4. partition_point (condition de fin de début) :- Cette fonction renvoie un itérateur pointant vers le point de partition du conteneur, c'est-à-dire le premier élément de la plage partitionnée [début) pour lequel la condition n'est pas vraie. Le conteneur doit déjà être partitionné pour que cette fonction fonctionne.

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

Sortir: 

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

Dans le code ci-dessus, les éléments pairs et impairs sont partitionnés et dans l'ordre croissant (triés). Pas toujours par ordre croissant, bien qu'ici les éléments (pairs et impairs) apparaissent dans un ordre croissant, tout comme le résultat après partition. si vect avait été { 217865 } après stable_partition(), ce serait  { 286175 }. L’ordre d’apparition est maintenu.
5. partition_copy (état début fin début1 début2) :- Cette fonction copie les éléments partitionnés dans les différents conteneurs mentionnés dans son argumentation. Il faut 5 arguments. Position de début et de fin du conteneur position de début du nouveau conteneur où les éléments doivent être copiés (éléments renvoyant vrai pour la condition) position de début du nouveau conteneur où d'autres éléments doivent être copiés (éléments renvoyant faux pour la condition) et la condition . Redimensionnement nouveaux conteneurs est nécessaire pour cette fonction.



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

Sortir: 

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