partial_sort_copy
PrototypePartial_sort_copy is an overloaded name; there are actually two partial_sort_copy functions. template <class InputIterator, class RandomAccessIterator> RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); template <class InputIterator, class RandomAccessIterator, class StrictWeakOrdering> RandomAccessIterator partial_sort_copy(InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); DescriptionPartial_sort_copy copies the smallest N elements from the range [first, last) to the range [result_first, result_first + N), where N is the smaller of last - first and result_last - result_first. The elements in [result_first, result_first + N) will be in ascending order.
The two versions of
The postcondition for the first version of
The return value is DefinitionDefined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.Requirements on typesFor the first version:
Preconditions
ComplexityApproximately(last - first) * log(N) comparisons, where N is the smaller of last - first and result_last - result_first. Exampleint A[] = {7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5}; const int N = sizeof(A) / sizeof(int); vector<int> V(4); partial_sort_copy(A, A + N, V.begin(), V.end()); copy(V.begin(), V.end(), ostream_iterator<int>(cout, " ")); // The printed result is "1 2 3 4". NotesSee alsopartial_sort, sort, stable_sort, binary_search, lower_bound, upper_bound, less<T>, StrictWeakOrdering, LessThanComparable | |||||||

