C++ STL algorithm, partial_sort_copy() 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_copy() to copy elements from a source range into a destination range where the source elements are ordered by either less than or another specified binary predicate. in C++ programming

To show: How to use the C++ algorithm, partial_sort_copy() to copy elements from a source range into a destination range where the source elements are ordered by either less than or another specified binary predicate. in C++ programming

 

// C++ STL algorithm, partial_sort_copy()

#include <vector>

#include <list>

#include <algorithm>

#include <functional>

#include <iostream>

using namespace std;

 

int main(void)

{

// container

vector <int> vec1, vec2;

list <int> lst;

// iterator

vector <int>::iterator Iter1, Iter2;

list <int>::iterator lst_Iter, lst_inIter;

int i, j;

 

// push data in range

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

vec1.push_back(i);

 

// shuffle the data randomly

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

 

// push the data individually

lst.push_back(6);

lst.push_back(5);

lst.push_back(2);

lst.push_back(3);

lst.push_back(4);

lst.push_back(1);

 

// print data

cout<<"vec1 vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

// print data

cout<<"\nlst list data: ";

for(lst_Iter = lst.begin(); lst_Iter!= lst.end(); lst_Iter++)

cout<<*lst_Iter<<" ";

cout<<endl;

// copying a partially sorted copy of lst list into vec1 vector

cout<<"\nOperation: partial_sort_copy(lst.begin(), lst.end(), vec1.begin(), vec1.begin()+3)"<<endl;

vector <int>::iterator result1;

result1 = partial_sort_copy(lst.begin(), lst.end(), vec1.begin(), vec1.begin()+3);

// print the data

cout<<"vec1 vector data: ";

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

cout<<*Iter1<<" ";

cout<<endl;

cout<<"The first vec1 vector element one position beyond the last lst list element inserted was "<<*result1<<endl;

// copying a partially sorted copy of lst list into vec2 vector

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

vec2.push_back(j);

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

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

vector <int>::iterator result2;

cout<<"Operation: partial_sort_copy(lst.begin(),lst.end(), vec2.begin(), vec2.begin()+6, greater<int>())"<<endl;

result2 = partial_sort_copy(lst.begin(), lst.end(), vec2.begin(), vec2.begin()+6, greater<int>());

cout<<"lst list into vec2 vector data: ";

for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

cout<<"The first vec2 vector element one position beyond the last lst list element inserted was "<<*result2<<endl;

return 0;

}

 

Output examples:

 

vec1 vector data: 4 1 6 2 0 5 7 3

lst list data: 6 5 2 3 4 1

Operation: partial_sort_copy(lst.begin(), lst.end(), vec1.begin(), vec1.begin()+3)

vec1 vector data: 1 2 3 2 0 5 7 3

The first vec1 vector element one position beyond the last lst list element inserted was 2

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

Operation: partial_sort_copy(lst.begin(),lst.end(), vec2.begin(), vec2.begin()+6, greater<int>())

lst list into vec2 vector data: 6 5 4 3 2 1 6 8 1 2

The first vec2 vector element one position beyond the last lst list element inserted was 6

Press any key to continue . . .

 

 

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