algorithm - STL C++

partial_sort()

Declaration

template <class RandomAccessIterator>
void partial_sort(
	RandomAccessIterator  xFirst,
	RandomAccessIterator  xEndOfSort,
	RandomAccessIterator  xLast
);

Description

This function partially sorts the elements in range from "xFirst" up through the one before "xLast" so that the elements up to the one before "xEndOfSort" are sorted and the elements after them are all greater than or equal to them. The overloaded version uses the function "xComp" as a comparison function.

Header Include

#include <algorithm>

Overloads

template <class RandomAccessIterator, class BinaryPred>
void partial_sort(
	RandomAccessIterator  xFirst,
	RandomAccessIterator  xEndOfSort,
	RandomAccessIterator  xLast,
	BinaryPred xComp
);

Example

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

int main()
{
	using namespace std;

	// Create a vector instance
	vector<int> qV;
	for (int i = 0; i < 20; i += 2) {
		qV.push_back(20 - i);
	}

	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 up to the fourth element
	partial_sort(qV.begin(), qV.begin() + 4, qV.end());

	// Output the result of the sort
	cout << "The Result: ";
	for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
		cout << *qIter << "  ";
	}
	cout << endl;

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

Output

partial_sort() Output
 

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