partition

分類在範圍內的元素加入兩個斷續文字範圍的集合,而這些項目是否在未滿足它的前一個一元運算子的述詞。

template<class BidirectionalIterator, class Predicate>
   BidirectionalIterator partition(
      BidirectionalIterator _First, 
      BidirectionalIterator _Last, 
      Predicate _Comp
   );

參數

  • _First
    解決雙向 Iterator 的第一個項目的位置會分割的範圍。

  • _Last
    解決一的雙向 Iterator 超過最後一個項目的位置是在要分割的範圍。

  • _Comp
    定義要滿足條件的使用者定義之述詞函式物件項目,則要分類。述詞會採用單一引數並傳回 truefalse

傳回值

解決雙向 Iterator 的第一個項目的位置在範圍不滿足述詞條件。

備註

參考的範圍必須是有效的,任何指標必須 dereferenceable,並在序列中最後一個位置開始可取得的會增加。

項目 ab 是相等的,當中,但不一定等於,則為,如果兩個 (a, *b)*是錯誤和 或b(, ),如果發生錯誤,其中是 參數所指定的述詞。partition 演算法不穩定,而且不保證相對排程對等項目來儲存。這個演算法 stable_ partition 儲存原始順序。

複雜的線性:有 (_Last – _First) _Comp 的應用程式,而且最多_Last (–) _First/2 交換。

範例

// alg_partition.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <iostream>

bool greater5 ( int value ) {
   return value >5;
}

int main( ) {
   using namespace std;
   vector <int> v1, v2;
   vector <int>::iterator Iter1, Iter2;

   int i;
   for ( i = 0 ; i <= 10 ; i++ )
   {
      v1.push_back( i );
   }
   random_shuffle( v1.begin( ), v1.end( ) );

   cout << "Vector v1 is ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Partition the range with predicate greater10
   partition ( v1.begin( ), v1.end( ), greater5 );
   cout << "The partitioned set of elements in v1 is: ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;
}

範例輸出

Vector v1 is ( 10 1 9 2 0 5 7 3 4 6 8 ).
The partitioned set of elements in v1 is: ( 10 8 9 6 7 5 0 3 4 2 1 ).

需求

標題: <algorithm>

命名空間: std

請參閱

參考

partition (STL Samples)

標準樣板程式庫