logo

STD :: Partition i C ++ STL

C ++ har en klass i sitt STL -algoritmerbibliotek som gör det möjligt för oss enkla partitionsalgoritmer med vissa inbyggda funktioner. Partition hänvisar till att dela delar av containrar beroende på ett givet tillstånd. 
Partitionsoperationer :
1. Partition (Beg End Condition) :- Denna funktion är van vid partition elemententillståndsbas nämns i sina argument.
2. IS_Partitioned (Beg End Condition) :- Den här funktionen returnerar booleska sant om behållaren är uppdelad annars returnerar falskt.

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

Produktion: 



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

I ovanstående kodpartitionsfunktionspartitioner är vektorn beroende på om ett element är jämnt eller udda till och med element uppdelas från udda element i ingen särskild ordning. 
3. Stable_Partition (Beg End Condition) :- Denna funktion är van vid partition elemententillståndsbas nämns i sina argument i Ett sådant sätt att elementens relativa ordning bevaras. .
4. Partition_point (Beg End Condition) :- den här funktionen returnerar en iterator som pekar på partitionspunkten av behållare, dvs det första elementet i det partitionerade intervallet [begend) för vilket tillstånd inte är sant. Behållaren bör redan delas upp för att denna funktion ska fungera.

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

Produktion: 

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

I ovanstående kod är jämn och udda element uppdelade och i den ökande ordningen (sorterad). Inte alltid i ökande ordning, men här elementen (jämn och udda) dök upp i ökad ordning så är resultatet efter partition. Om vekt skulle ha varit {217865} efter stabil_partition () skulle det vara {286175}. Ordningen på utseende upprätthålls.
5. PARTITION_COPY (Beg End Beg1 Beg2 Condition) :- den här funktionen kopierar de partitionerade elementen I de olika containrarna som nämns i dess argument. Det tar 5 argument. Början och slutpositionen för containernens början av ny container där element måste kopieras (element som återgår sant för tillstånd) Börjposition för ny container där andra element måste kopieras (element som returnerar falskt för tillstånd) och tillståndet . Storleksändring Nya containrar är nödvändig för den här funktionen.



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

Produktion: 

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