std :: partició en C ++ STL
C ++ té una classe a la seva biblioteca d’algoritmes STL que ens permet algoritmes de partició fàcils mitjançant determinades funcions integrades. La partició es refereix a l'acte d'elements divisòries dels contenidors en funció d'una condició determinada.
Operacions de partició :
1. Partició (condició de finalització de beg) :- Aquesta funció s'utilitza Partició Els elements al damunt de Bases de la condició esmentat en els seus arguments.
2. :- Aquesta funció retorna boolean És cert si es divideix el contenidor Altrament torna fals.
// 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 ; }
Sortida:
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7
A la funció de partició de codi anterior, el vector depèn de si un element és uniforme o estrany, fins i tot, els elements es reparteixen amb elements estranys en cap ordre particular.
3. Stable_partition (condició final de beg) :- Aquesta funció s'utilitza Partició Els elements al damunt de Bases de la condició esmentat en els seus arguments a De la manera que es conserva l’ordre relatiu dels elements. .
4. Partition_point (condició final de beg) :- Aquesta funció Retorna un iterador que apunta al punt de partició del contenidor, és a dir, el primer element de la gamma particionada [Begend) per a la qual no és certa. El contenidor ja s’ha de participar perquè aquesta funció funcioni.
// 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 ; }
Sortida:
The partitioned vector is : 2 6 8 1 5 7 The vector elements returning true for condition are : 2 6 8
En el codi anterior, els elements estranys es reparteixen i en l'ordre creixent (ordenat). No sempre en ordre creixent, tot i que aquí els elements (fins i tot i estranys) van aparèixer en un ordre augmentat, també ho és el resultat després de la partició. Si el VECT hauria estat {217865} després de stable_partition () seria {286175}. Es manté l’ordre d’aparença.
5. Partition_copy (condició Beg End Beg1 Beg2) :- Aquesta funció copia els elements particionats En els diferents contenidors esmentats en els seus arguments. Es necessita 5 arguments. Posició inicial i final del contenidor Posició inicial del nou contenidor on s’han de copiar elements (elements que tornen a la condició) Posició inicial del nou contenidor on s’han de copiar altres elements (elements que tornen falsos per a la condició) i la condició . Refonçar Contenidors nous és necessari per a aquesta funció.
// 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 ; }
Sortida:
The elements that return true for condition are : 2 6 8 The elements that return false for condition are : 1 5 7