I have forgotten

• https://me.yahoo.com

# stable_partition

Same as partition(), but preserves the relative order of matching and nonmatching elements
View version details

### Key Facts

Gyroscopic Couple: The rate of change of angular momentum () = (In the limit).
• = Moment of Inertia.
• = Angular velocity
• = Angular velocity of precession.

Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

## Definition

The stable_partition() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

## Interface

#include <algorithm>
template < class BidirectionalIterator, class Predicate >
BidirectionalIterator stable_partition(
BidirectionalIterator first,
BidirectionalIterator last,
Predicate pred
);

Parameters:
Parameter Description
first A bidirectional iterator addressing the position of the first element in the range to be partitioned
last A bidirectional iterator addressing the position one past the final element in the range to be partitioned
pred User-defined predicate function object that defines the condition to be satisfied if an element is to be classified. A predicate takes single argument and returns true or false

## Description

Stable_partition function does the same thing that partition, except that the relative order between elements is kept. Thus, stable partition uses more computation time than partition.

## Return Value

Returns a bidirectional iterator addressing the position of the first element in the range to not satisfy the predicate condition.

## Complexity

Performs exactly last - first applications of the predicate and at most (last -first)*log(last - first) swaps if there is insufficient memory or linear number of swaps if sufficient memory is available.

### References

Example:
##### Example - stable_partition algorithm
Problem
This program illustrates the functionality of stable_partition() algorithm.
Workings
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

bool greaterthan(int value )
{    return value > 5;    }

int main()
{
vector <int> vec1, vec2;
vector <int>::iterator Iter1, Iter2, result;
int i,j;
for(i = 0; i <= 10; i++)
vec1.push_back(i);
for(j = 0; j <= 4; j++)
vec1.push_back(3);
random_shuffle(vec1.begin(), vec1.end());
cout <<"Random shuffle vector vec1 data:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
// partition the range with predicate greater than 5...
result = stable_partition (vec1.begin(), vec1.end(), greaterthan);
cout <<"\nThe partitioned set of elements in vec1 is:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
cout <<"\nThe first element in vec1 fail to satisfy "
"the predicate greaterthan is:\n "<<*result<<endl;

return 0;
}
Solution
Output:

Random shuffle vector vec1 data:
3 1 9 2 0 3 7 3 4 3 8 5 3 3 10 6

The partitioned set of elements in vec1 is:
9 7 8 10 6 3 1 2 0 3 3 4 3 5 3 3

The first element in vec1 fail to satisfy the predicate greaterthan is:
3
References