.\" ident @(#)partition.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH partition 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2partition\fP \ - Places all of the entities that satisfy the given predicate before all of the entities that do not. .SH SYNOPSIS .RE .RS 0 #include .br template .br BidirectionalIterator .br partition (BidirectionalIterator first, .RE .RS 10 BidirectionalIterator last, .br Predicate pred); .SH DESCRIPTION For the range \f2[first, last)\fP, the partition algorithm places all elements that satisfy \f2pred\fP before all elements that do not satisfy \f2pred\fP. It returns an iterator that is one past the end of the group of elements that satisfy \f2pred\fP. In other words, partition returns \f2i\fP such that for any iterator \f2j\fP in the range \f2[first, i)\fP, \f2pred(*j) == true\fP, and, for any iterator \f2k\fP in the range \f2[i, last)\fP, \f2pred(*j) == false\fP. Note that partition does not necessarily maintain the relative order of the elements that match and elements that do not match the predicate. Use the algorithm stable_partition if relative order is important. .SH COMPLEXITY The partition algorithm does at most \f2(last - first)/2 \fPswaps, and applies the predicate exactly \f2last - first \fPtimes. .SH EXAMPLE .RE .RS 0 // .br // prtition.cpp .br // .RE .RS 1 #include .br #include .br #include .br #include .RE .RS 0 using namespace std; .br .RE .RS 1 // .br // Create a new predicate from unary_function. .br // .RE .RS 0 template .br class is_even : public unary_function .RE .RS 1 { .RE .RS 2 public: .br bool operator()(const Arg& arg1) { return (arg1 % 2) .RE .RS 18 == 0; } .RE .RS 1 }; .RE .RS 0 .br int main () .RE .RS 1 { .RE .RS 3 // .br // Initialize a deque with an array of integers. .br // .RE .RS 2 int init[10] = { 1,2,3,4,5,6,7,8,9,10 }; .br deque d1(init+0, init+10); .br deque d2(init+0, init+10); .RE .RS 3 // .br // Print out the original values. .br // .RE .RS 2 cout << "Unpartitioned values: " << "\\t\\t"; .br copy(d1.begin(), d1.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl; .RE .RS 3 // .br // A partition of the deque according to even/oddness. .br // .br partition(d2.begin(), d2.end(), is_even()); .br // .br // Output result of partition. .br // .RE .RS 2 cout << "Partitioned values: " << "\\t\\t"; .br copy(d2.begin(), d2.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl; .RE .RS 3 // .br // A stable partition of the deque according .br // to even/oddness. .br // .RE .RS 2 stable_partition(d1.begin(), d1.end(), is_even()); .RE .RS 3 // .br // Output result of partition. .br // .RE .RS 2 cout << "Stable partitioned values: " << "\\t"; .br copy(d1.begin(), d1.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br Unpartitioned values: 1 2 3 4 5 6 7 8 9 10 .br Partitioned values: 10 2 8 4 6 5 7 3 9 1 .br Stable partitioned values: 2 4 6 8 10 1 3 5 7 9 .SH WARNINGS If your compiler does not support default template parameters, then you always need to supply the \f2Allocator\fP template argument. For instance, you need to write: \f2deque >\fP instead of: \f2deque\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO stable_partition