C++ STL algorithm, partial_sort() program example

 

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++ partial_sort() to arrange a specified number of the smaller elements in a range into a non-descending order or according to an ordering criterion specified by a binary predicate in C++ programming

To show: How to use the C++ algorithm, partial_sort() to arrange a specified number of the smaller elements in a range into a non-descending order or according to an ordering criterion specified by a binary predicate in C++ programming

 

// C++ STL algorithm, partial_sort()

#include <vector>

#include <algorithm>

// for greater<int>()

#include <functional>

#include <iostream>

using namespace std;

 

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

bool great(int elem1, int elem2)

{return elem1 > elem2;}

 

int main(void)

{

// container

vector <int> vec1;

// iterator

vector <int>::iterator Iter1;

int i, j;

 

// push data in range

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

vec1.push_back(i);

 

// push more data

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

vec1.push_back(j);

 

// print the data

cout<<"vec1 vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

cout<<"\nOperation: partial_sort(vec1.begin(), vec1.begin()+ 5, vec1.end())"<<endl;

partial_sort(vec1.begin(), vec1.begin() + 5, vec1.end());

cout<<"Partially sorted vec1 vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// to partially sort in descending order, specify binary predicate

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

partial_sort(vec1.begin(), vec1.begin()+4, vec1.end(), greater<int>());

cout<<"Partially re-sorted (greater()) vec1 vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// a user-defined binary predicate can also be used

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

partial_sort(vec1.begin(), vec1.begin()+8, vec1.end(), great);

cout<<"Partially re-sorted (great()) vec1 vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

vec1 vector data: 10 11 12 13 14 15 16 0 1 2 3 4 5

Operation: partial_sort(vec1.begin(), vec1.begin()+ 5, vec1.end())

Partially sorted vec1 vector data: 0 1 2 3 4 15 16 14 13 12 11 10 5

Operation: partial_sort(vec1.begin(), vec1.begin()+4, vec1.end(), greater<int>())

Partially re-sorted (greater()) vec1 vector data: 16 15 14 13 0 1 2 3 4 12 11 10 5

Operation: partial_sort(vec1.begin(), vec1.begin()+8, vec1.end(), great)

Partially re-sorted (great()) vec1 vector data: 16 15 14 13 12 11 10 5 0 1 2 3 4

Press any key to continue . . .

 

 

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