partial_sort_copy

Syntax:

    #include <algorithm>
    random_access_iterator partial_sort_copy( input_iterator start, input_iterator end, random_access_iterator result_start, random_access_iterator result_end );
    random_access_iterator partial_sort_copy( input_iterator start, input_iterator end, random_access_iterator result_start, random_access_iterator result_end, StrictWeakOrdering cmp );

The partial_sort_copy() algorithm behaves like partial_sort(), except that instead of partially sorting the range in-place, a copy of the range is created and the sorting takes place in the copy. The initial range is defined by [start,end) and the location of the copy is defined by [result_start,result_end).

partial_sort_copy() returns an iterator to the end of the copied, partially- sorted range of elements.

Example:

// partial_sort_copy example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
bool myfunction (int i,int j) { return (i<j); }
 
int main () {
  int myints[] = {9,8,7,6,5,4,3,2,1};
  vector<int> myvector (5);
  vector<int>::iterator it;
 
  // using default comparison (operator <):
  partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end());
 
  // using function as comp
  partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end(), myfunction);
 
  // print out content:
  cout << "myvector contains:";
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    cout << " " << *it;
 
  cout << endl;
 
  return 0;
}

Output: myvector contains: 1 2 3 4 5

Related Topics: binary_search, is_sorted, partial_sort, sort, stable_sort