++


partition()


template < class BidirectionalIterator, class UnaryPredicate >

BidirectionalIterator

partition(

BidirectionalIterator first,

BidirectionalIterator last, UnaryPredicate pred );

partition() [first,last). , pred true, , false. , {0,1,2,3,4,5,6} , ,   {0,2,4,6} {1,3,5}. , , , .. 4 2, 5 1. stable_partition(), .

#include <algorithm>

#include <vector>

#include <iostream.h>

class even_elem {

public:

bool operator()( int elem )

{ return elem%2 ? false : true; }

};

/*

* :

:

29 23 20 22 17 15 26 51 19 12 35 40

, :

40 12 20 22 26 15 17 51 19 23 35 29

, 25:

12 23 20 22 17 15 19 51 26 29 35 40

*/

 

int main()

{

const int ia_size = 12;

int ia[ia_size] = { 29,23,20,22,17,15,26,51,19,12,35,40 };

 

vector< int, allocator > vec( ia, ia+ia_size );

ostream_iterator< int > outfile( cout, " " );

 

cout << " : \n";

copy( vec.begin(), vec.end(), outfile ); cout << endl;

cout << ", :\n";

partition( &ia[0], &ia[ia_size], even_elem() );

copy( ia, ia+ia_size, outfile ); cout << endl;

 

cout << ", 25:\n";

partition( vec.begin(), vec.end(), bind2nd(less<int>(),25) );

copy( vec.begin(), vec.end(), outfile ); cout << endl;

}




- -  - -  - -