Here is the source code of the C++ program which rearranges the container elements using stable_partition() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to rearrange container elements using stable_partition() algorithm
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isGreaterThanFive(int i)
{
return i > 5;
}
int main() {
vector<int> v = {5, 1, 6, 2, 4, 3, 9, 8, 10, 7};
vector<int>::iterator it;
cout << "Vector : ";
for(vector<int>::iterator i = v.begin(); i != v.end(); i++)
cout << *i << " ";
cout << "\n";
it = stable_partition(v.begin(), v.end(), isGreaterThanFive);
cout << "Elements greater than 5 : ";
for(vector<int>::iterator i = v.begin(); i != it; i++)
cout << *i << " ";
cout << "\n";
cout << "Elements less than or equal to 5 : ";
for(vector<int>::iterator i = it; i != v.end(); i++)
cout << *i << " ";
cout << "\n";
}
$ gcc test.cpp $ a.out Vector : 5 1 6 2 4 3 9 8 10 7 Elements greater than 5 : 6 9 8 10 7 Elements less than or equal to 5 : 5 1 2 4 3