algorithm - STL C++

partial_sort_copy()

Declaration

template <class InIter, class RandomIter>
void partial_sort_copy(
	InIter xSrcFirst,
	InIter xSrcLast,
	RandomIter xDestFirst,
	RandomIter xDestLast
);

Description

This function sorts and copies the partially sorted elements in range from "xSrcFirst" up through the one before "xSrcLast" that fit into the range from "xDestFirst" up through the one before "xDestLast." The elements must have a partial ordering. If the elements are totally ordered, the elements will be fully ordered when they are copied. The overloaded version uses the function "xComp" as a comparison function.

Header Include

#include <algorithm>

Overloads

template <class InIter, class RandomIter, class BinaryPred>
void partial_sort_copy(
	InIter xSrcFirst,
	InIter xSrcLast,
	RandomIter xDestFirst,
	RandomIter xDestLast,
	BinaryPred xComp
);

Example

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	using namespace std;

	// Create two vector instances
	vector<int> qV;
	for (int i = 0; i < 20; i += 2) {
		qV.push_back(20-i);
	}
	vector<int> qTarget(5);

	vector<int>::iterator qIter;
	// Output the vector
	cout << "The Vector: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Partial sort the vector
	partial_sort_copy(qV.begin(), qV.end(), qTarget.begin(), qTarget.end());

	// Output the resulting sorted vector
	cout << "The Sorted Copy: ";
	for (qIter = qTarget.begin(); qIter != qTarget.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

partial_sort_copy() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.