I can suggest you to go through the following code of lines that can help's you to understand the partial_sort_copy() function of the C++ language more easily and quickly :
Code:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
typedef std::vector<int, std::allocator<int> > Vector;
typedef std::ostream_iterator<int, char,
std::char_traits<char> > Iter;
const Vector::value_type b[] = {
17, 3, 5, -4, 1, 12, -10, -1, 14, 7,
-6, 8, 15, -11, 2, -2, 18, 4, -3, 0
};
Vector w1 (b + 0, b + sizeof b / sizeof *b);
std::cout << "For the vector: ";
std::copy (w1.begin (), w1.end (), Iter (std::cout, " "));
std::partial_sort (w1.begin (), w1.begin () + 7,
w1.end ());
std::cout << "\n\nA partial_sort of seven elements "
<< "gives: \n ";
std::copy (w1.begin (), w1.end (), Iter (std::cout, " "));
std::cout << std::endl;
Vector w2 (Vector::size_type (10), 0);
std::partial_sort_copy (w1.begin () + 10, w1.end (),
w2.begin (), w2.end ());
std::cout
<< "\nA partial_sort_copy of the last ten elements "
<< "gives: \n ";
std::copy (w2.begin (), w2.end (), Iter (std::cout, " "));
std::cout << std::endl;
return 0;
}
Program Output:
Code:
For the vector: 17 3 5 -4 1 12 -10 -1 14 7 -6 8 15 -11 2 -2
18 4 -3 0
A partial_sort of seven elements gives:
-11 -10 -6 -4 -3 -2 -1 17 14 12 7 8 15 5 3 2 18 4 1 0
A partial_sort_copy of the last ten elements gives:
0 1 2 3 4 5 7 8 15 18
Bookmarks