C++ STL algorithm, partition() program example

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ partition() to classify elements in a range into two disjoint sets, with those elements satisfying a unary predicate preceding those that fail to satisfy it in C++ programming

To show: How to use the C++ algorithm, partition() to classify elements in a range into two disjoint sets, with those elements satisfying a unary predicate preceding those that fail to satisfy it in C++ programming

// C++ STL algorithm, partition()

#include <vector>

#include <algorithm>

#include <iostream>

using namespace std;

// user defined

bool great(int value)

{return value >3;}

int main(void)

{

// container

vector <int> vec1, vec2;

// iterator

vector <int>::iterator Iter1, Iter2;

int i;

// push data

for(i = 0; i <= 10; i++)

vec1.push_back(i);

// print data

cout<<"vec1 vector data is: ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

cout<<"\nOperation: random_shuffle(vec1.begin(), vec1.end())"<<endl;

random_shuffle(vec1.begin(), vec1.end());

cout<<"vec1 vector data is: ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// partition the range with predicate great

cout<<"\nOperation: partition(vec1.begin(), vec1.end(), great)"<<endl;

partition(vec1.begin(), vec1.end(), great);

cout<<"The partitioned set of elements in vec1 vector is: ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

return 0;

}

Output examples:

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

Operation: random_shuffle(vec1.begin(), vec1.end())

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

Operation: partition(vec1.begin(), vec1.end(), great)

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

Press any key to continue . . .