C++ STL の std::partition

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

出力: 

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(開始条件) :- この機能 パーティションポイントを指すイテレータを返します。 コンテナの、つまり、条件が true ではないパーティション化された範囲 [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 

上記のコードでは、偶数要素と奇数要素が昇順 (ソート) で分割されています。必ずしも昇順であるわけではありませんが、ここでは要素 (偶数と奇数) が昇順で表示されており、分割後の結果も同様です。 Stable_partition() の後の vect が { 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