algorithm - STL C++

stable_partition()

Declaration

template <class BidirIter, class Predicate>
BidirIter stable_partition(
	BidirIter xFirst,
	BidirIter xLast,
	Predicate xTest
);

Description

This function uses the function "xTest" to order the elements in range from "xFirst" up through the one before "xLast" into two sets so that each element in the first set returns true when passed into "xTest" while each element in the second set returns false. This function is similar to partition(), except that it also keeps the elements in their original relative order within each partition.

Header Include

#include <algorithm>

Example

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

bool GreaterThanZ(char cTest) {
	return (cTest > 'Z');
}

int main()
{
	using namespace std;

	// Create a vector instance
	vector<char> qV;
	qV.push_back('X');
	qV.push_back('o');
	qV.push_back('a');
	qV.push_back('X');
	qV.push_back('.');
	qV.push_back('n');
	qV.push_back('e');
	qV.push_back('t');

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

	// Partition the vector
	vector<char>::iterator qPartEnd;
	qPartEnd = stable_partition(qV.begin(), qV.end(), GreaterThanZ);

	// Output the patitioned vector
	cout << "Partitioned: ";
	for (qIter = qV.begin(); qIter != qPartEnd; ++qIter) {
		cout << *qIter;
	}
	cout << " | ";
	for (qIter = qPartEnd; qIter != qV.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

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

Output

stable_partition() Output
 

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