logo

C++ STL의 std::파티션

C++에는 STL 알고리즘 라이브러리에 특정 내장 함수를 사용하여 쉽게 분할 알고리즘을 사용할 수 있는 클래스가 있습니다. 파티션이란 주어진 조건에 따라 컨테이너의 요소를 나누는 행위를 말합니다. 
파티션 작업 :
1. 파티션(종료 조건 요청) :- 이 기능은 다음과 같은 용도로 사용됩니다. 요소를 분할하다 ~에 조건의 기초 주장에서 언급했습니다.
2. is_partitioned(종료 조건 요청) :- 이 함수는 부울값을 반환합니다. 컨테이너가 분할된 경우 true 그렇지 않으면 false를 반환합니다.

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

산출: 

오픈 소스 OS의 예는 다음과 같습니다.
Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7

위의 코드 분할 함수에서 요소가 짝수인지 홀수인지에 따라 벡터를 분할합니다. 짝수 요소는 특별한 순서 없이 홀수 요소에서 분할됩니다. 
3. stable_partition(종료 조건 요청) :- 이 기능은 다음과 같은 용도로 사용됩니다. 요소를 분할하다 ~에 조건의 기초 의 주장에서 언급됨 요소의 상대적 순서가 유지되는 방식입니다. .
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가 stable_partition() 이후에 { 217865 }라면  { 286175 }가 됩니다. 등장 순서는 유지됩니다.
5. partition_copy(beg end beg1 beg2 조건) :- 이 기능 분할된 요소를 복사합니다. 인수에서 언급된 다른 컨테이너에 있습니다. 5개의 인수가 필요합니다. 컨테이너의 시작 및 끝 위치 요소를 복사해야 하는 새 컨테이너의 시작 위치(조건에 대해 true를 반환하는 요소) 다른 요소를 복사해야 하는 새 컨테이너의 시작 위치(조건에 대해 false를 반환하는 요소) 및 조건 . 크기 조정 새로운 컨테이너 필요하다 이 기능을 위해.

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