logo

std :: розділ у c ++ stl

C ++ має клас у своїй бібліотеці алгоритмів STL, що дозволяє нам легкими алгоритмами розділення, використовуючи певні вбудовані функції. Розділ відноситься до акта ділення елементів контейнерів залежно від заданої умови. 
Операції з розділом :
1. Розділ (умова BEG END) :- Ця функція використовується розділити елементи на основа стану згадується в його аргументах.
2. is_partiteed (BEG END умова) :- Ця функція повертає булеву Правда, якщо контейнер розділений інакше повертається помилково.

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

Вихід: 



порядок у випадковому порядку в sql
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7

У вищезазначених розділах розділів коду вектор залежно від того, чи є елемент навіть або непарними елементами, розподілені з непарних елементів у жодному конкретному порядку. 
3. STABLE_PARTITION (BEG END CONDION) :- Ця функція використовується розділити елементи на основа стану згадується в його аргументах у Такий спосіб збереження відносного порядку елементів. .
4. partition_point (умову кінцевої умови) :- Ця функція Повертає ітератор, вказуючи на пункт розділу контейнера, тобто перший елемент у розділеному діапазоні [Begend), для якого умова не відповідає дійсності. Контейнер вже повинен бути розділений для роботи цієї функції.

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

Вихід: 

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

У наведеному вище коді і непарні елементи розподіляються та в порядку зростання (відсортовано). Не завжди в зростаючому порядку, хоча тут елементи (навіть і непарні) з'являлися в збільшенні порядку, тому результат після розділу. Якби VECT був би {217865} після стабільного_partition (), це було б {286175}. Порядок зовнішнього вигляду підтримується.
5. partition_copy (Beg end Beg1 Beg2 умова) :- Ця функція копіює розділені елементи в різних контейнерах, згаданих у його аргументах. Це займає 5 аргументів. Початок і закінчення положення контейнера початкового положення нового контейнера, де потрібно копіювати елементи (елементи, що повертаються вірно для умови) Початкове положення нового контейнера, де слід скопіювати інші елементи (елементи, що повертаються помилковими для умови) та умова . Зміна розміру Нові контейнери необхідно для цієї функції.



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

Вихід: 

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