Users Online
· Guests Online: 37
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
stable_partition() Algorithm in C++
stable_partition() Algorithm in C++
This C++ program rearranges the container elements using stable_partition() algorithm. The algorithm rearranges the elements of the container such that the elements for which predicate returns true precede the elements for which the predicate returns false. The relative order of the elements is preserved.
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
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.