C++ STL algorithm, nth_element() code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

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++ nth_element() to partition a range of elements, correctly locating the nth element of the sequence in the range so that all the elements in front of it are less than or equal to it and all the elements that follow it in the sequence are greater than or equal to it in C++ programming

To show: How to use the C++ STL algorithm, nth_element() to partition a range of elements, correctly locating the nth element of the sequence in the range so that all the elements in front of it are less than or equal to it and all the elements that follow it in the sequence are greater than or equal to it in C++ programming

 

// C++ STL algorithm, nth_element()

#include <vector>

#include <algorithm>

// for greater<int>()

#include <functional>

#include <iostream>

using namespace std;

 

// user defined function, return whether first element is greater than the second

bool great(int elem1, int elem2)

{return (elem1 > elem2);}

 

int main(void)

{

// container

vector <int> vec;

// iterator

vector <int>::iterator Iter1;

int i, j, k;

 

// pushing data in ranges

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

vec.push_back(i);

for(j = 10; j <= 15; j++)

vec.push_back(j);

for(k = 20; k <= 25; k++)

vec.push_back(k);

 

// print the data

cout<<"vec vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

cout<<"\nOperation: nth_element(vec.begin(), vec.begin()+3, vec.end())"<<endl;

nth_element(vec.begin(), vec.begin()+3, vec.end());

cout<<"Position 3 partitioned vec vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// to sort in descending order, specify binary predicate

cout<<"\nOperation: nth_element(vec.begin(), vec.begin()+4, vec.end(),greater<int>())"<<endl;

nth_element(vec.begin(), vec.begin()+4, vec.end(), greater<int>());

cout<<"Position 4 partitioned (greater()) vec vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

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

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

cout<<"Shuffled vec vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// a user-defined binary predicate...

cout<<"\nOperation: nth_element(vec.begin(), vec.begin() + 5, vec.end(), great)"<<endl;

nth_element(vec.begin(), vec.begin() + 5, vec.end(), great);

cout<<"Position 5 partitioned (great()) vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

vec vector data: 0 1 2 3 4 5 10 11 12 13 14 15 20 21 22 23 24 25

Operation: nth_element(vec.begin(),vec.begin()+3, vec.end())

Position 3 partitioned vec vector data: 0 1 2 3 4 5 10 11 12 13 14 15 20 21 22 23 24 25

Operation: nth_element(vec.begin(), vec.begin()+4, vec.end(),greater<int>())

Position 4 partitioned (greater()) vec vector data: 25 24 23 22 21 20 15 14 13 12 11 10 5 4 3 2 1 0

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

Shuffled vec vector data: 5 24 12 23 25 10 14 0 21 2 13 20 3 4 11 1 15 22

Operation: nth_element(vec.begin(), vec.begin() + 5, vec.end(), great)

Position 5 partitioned (great()) vector data: 25 24 23 22 21 20 15 14 13 12 11 10 5 4 3 2 1 0

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index