partition

在分类范围的元素分为两相交集,并且这些元素满足前面无法满足该类型的一元谓词。

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

参数

  • _First
    处理第一元素位置的双向迭代器将区域的范围。

  • _Last
    寻址最终元素的双向迭代器位置将一个分区的范围。

  • _Comp
    定义将满足的条件的用户定义谓词函数的对象元素,则将类。 谓词采用单个参数并返回 truefalse

返回值

处理第一元素位置的双向迭代器在范围未满足谓词条件。

备注

引用的范围必须是有效的;所有指针必须 dereferenceable,然后在序列中的最后位置从开始来访问通过递增。

元素 a and b 等效,但不必须等于,因此,如果 Pr (ab) 为 false 且 Pr (ba) 错误,其中 Pr 为参数指定谓词。 分区 算法不是稳定的并不保证相对顺序等效元素将保留。 算法 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 ).

要求

标头: <算法>

命名空间: std

请参见

参考

partition(STL 示例)

标准模板库