.\" ident @(#)stable_partition.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH stable_partition 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2stable_partition\fP \ - Places all of the entities that satisfy the given predicate before all of the entities that do not, while maintaining the relative order of elements in each group. .SH SYNOPSIS .br #include .br template .RE .RS 1 BidirectionalIterator .RE .RS 0 stable_partition (BidirectionalIterator first, .RE .RS 21 BidirectionalIterator last, .br Predicate pred); .SH DESCRIPTION For the range \f2[first, last)\fP, the stable_partition algorithm places all elements that satisfy \f2pred\fP before all elements that do not satisfy it. It returns an iterator \f2i\fP that is one past the end of the group of elements that satisfies \f2pred\fP. In other words stable_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. The relative order of the elements in both groups is preserved. The partition algorithm can be used when it is not necessary to maintain the relative order of elements within the groups that do and do not match the predicate. .SH COMPLEXITY The stable_partition algorithm does at most \f2(last - first) * log(last - first)\fP swaps and applies the predicate exactly \f2last - first\fP times. .SH EXAMPLE .RE .RS 0 // .br // prtition.cpp .br // .br #include .br #include .br #include .br #include .br using namespace std; .br .br //Create a new predicate from unary_function .br template .br class is_even : public unary_function .br { .br public: .br bool operator()(const Arg& arg1) .br { .br return (arg1 % 2) == 0; .RE .RS 3 } .RE .RS 1 }; .RE .RS 0 .br .br int main() .br { .br //Initialize a deque with an array of ints .br int init[10] = {1,2,3,4,5,6,7,8,9,10}; .br deque d(init, init+10); .br .br //Print out the original values .br cout << "Unpartitioned values: " << endl << " "; .br copy(d.begin(),d.end(), .RE .RS 4 ostream_iterator(cout," ")); .RE .RS 0 cout << endl << endl; .br .br //Partition the deque according to even/oddness .br stable_partition(d.begin(), d.end(), is_even()); .br .br //Output result of partition .br cout << "Partitioned values: " << endl << " "; .br copy(d.begin(),d.end(), .RE .RS 4 ostream_iterator(cout," ")); .RE .RS 0 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 partition