This C++ program demonstrates using partial_sort_copy() algorithm. The algorithm partially sorts the container elements and copies the sorted elements into another container according to the capacity of the second container.
Here is the source code of the C++ program which demonstrates using partial_sort_copy() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to demonstrate the partial_sort_copy() algorithm
*/
#include <algorithm>
#include <vector>
#include <functional>
#include <iostream>
using namespace std;
void print(vector<int> & v)
{
vector<int>::iterator it;
for (it = v0.begin(); it != v0.end(); it++) {
cout << *it << ' ';
}
cout << '\n';
}
int main()
{
vector<int> v0{4, 2, 5, 1, 3};
vector<int> v1{10, 11, 12};
vector<int> v2{10, 11, 12, 13, 14, 15, 16};
cout << "v0 : ";
print(v0);
cout << "v1 : ";
print(v1);
cout << "v2 : ";
print(v2);
it = partial_sort_copy(v0.begin(), v0.end(), v1.begin(), v1.end());
cout << "Writing v0 to v1 in ascending order gives: ";
print(v1);
it = partial_sort_copy(v0.begin(), v0.end(), v2.begin(), v2.end(),
std::greater<int>());
cout << "Writing v0 to v2 in descending order gives: ";
print(v2);
}
$ gcc test.cpp $ a.out v0 : 4 2 5 1 3 v1 : 10 11 12 v2 : 10 11 12 13 14 15 16 Writing v0 to v1 in ascending order gives: 1 2 3 Writing v0 to v2 in descending order gives: 5 4 3 2 1 15 16
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Apply for C++ Internship
- Practice Programming MCQs
- Check Programming Books
- Apply for Computer Science Internship
- Check C++ Books