C++里面 partition函数怎么用
发布网友
发布时间:2022-05-14 01:30
我来回答
共1个回答
热心网友
时间:2023-11-19 04:40
//要保证顺序的话还是用stable_partition好了
//partition返回是指向 范围中第一个不满足指定条件的 bidirectional iterator
//第三个参数是条件函数,该函数只有一个参数,返回值是true或false
#include <vector>
#include <algorithm>
#include <iostream>
bool greater5 ( int value ) {
return value >5;
}
void main( ) {
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1;
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 ( 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;
}