logo

C++ STL'de std::partition

C++'ın STL algoritma kitaplığında, belirli yerleşik işlevleri kullanarak kolay bölümleme algoritmaları oluşturmamıza olanak tanıyan bir sınıfı vardır. Bölme, belirli bir duruma bağlı olarak kapların elemanlarının bölünmesi eylemini ifade eder. 
Bölümleme işlemleri :
1. bölüm(bitiş koşulu için yalvarıyorum) : - Bu işlev şunun için kullanılır: elemanları bölmek Açık koşulun temeli argümanlarında dile getirildi.
2. is_partitioned(bitiş koşulu için yalvar) :- Bu işlev boole değeri döndürür kapsayıcı bölümlenmişse doğru aksi takdirde false değerini döndürür.

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

Çıkış: 



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

Yukarıdaki kodda bölme fonksiyonu, bir elemanın çift mi yoksa tek mi olduğuna bağlı olarak vektörü bölümlere ayırır. Çift elemanlar tek elemanlardan belirli bir sıraya göre bölünür. 
3. stabil_partition(son koşul için yalvar) : - Bu işlev şunun için kullanılır: elemanları bölmek Açık koşulun temeli argümanlarında bahsedilen elemanların göreceli sırası korunacak şekilde. .
4. partition_point(bitiş koşulu için yalvar) :- Bu fonksiyon bölüm noktasına işaret eden bir yineleyici döndürür kapsayıcının yani bölümlenmiş aralıktaki [begend] koşulun doğru olmadığı ilk öğe. Bu işlevin çalışması için kapsayıcının zaten bölümlenmiş olması gerekir.

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

Çıkış: 

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

Yukarıdaki kodda çift ve tek elemanlar bölümlendirilmiş ve artan sırada (sıralanmıştır). Her zaman artan sırada olmasa da burada öğeler (çift ve tek) artan sırada göründü ve bölümlemeden sonraki sonuç da öyle. eğer vect, stable_partition()'dan sonra { 217865 } olsaydı,  { 286175 } olurdu. Görünüm sırası korunur.
5. partition_copy(beg end beg1 beg2 koşulu) :- Bu fonksiyon bölümlenmiş öğeleri kopyalar argümanlarında belirtilen farklı kaplarda. 5 argüman alır. Kabın başlangıç ​​ve bitiş konumu, öğelerin kopyalanması gereken yeni kabın başlangıç ​​konumu (koşul için true değerini döndüren öğeler), diğer öğelerin kopyalanması gereken yeni kabın başlangıç ​​konumu (koşul için false değerini döndüren öğeler) ve koşul . Yeniden boyutlandırma yeni konteynerler gerekli bu işlev için.



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

Çıkış: 

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