Standard C++ Library Class Reference

group of elements that satisfy pred. In other words stable_partition returns i such that for any
iterator j in the range [first, i), pred(*j) == true, and for any iterator k in the range [i, last),
pred(*j) == false. 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.
Complexity
The stable_partition algorithm does at most (last - first) * log(last - first) swaps. and applies the
predicate exactly last - first times.
Example
//
// prtition.cpp
//
#include <functional>
#include <deque>
#include <algorithm>
#include <iostream.h>
//Create a new predicate from unary_function
template<class Arg>
class is_even : public unary_function<Arg, bool>
{
public:
bool operator()(const Arg& arg1)
{
return (arg1 % 2) == 0;
}
};
int main()
{
//Initialize a deque with an array of ints
int init[10] = {1,2,3,4,5,6,7,8,9,10};
deque<int> d(init, init+10);
//Print out the original values
cout << "Unpartitioned values: " << endl << " ";
copy(d.begin(),d.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;
//Partition the deque according to even/oddness
stable_partition(d.begin(), d.end(), is_even<int>());
//Output result of partition
cout << "Partitioned values: " << endl << " ";
copy(d.begin(),d.end(),ostream_iterator<int>(cout," "));