algorithm - STL C++

prev_permutation()

Declaration

template <class BidirIterator>
bool prev_permutation(
	BidirIterator xFirst,
	BidirIterator xLast
);

Description

This function returns bool to indicate whether the entries in the range from "xFirst" up through the one before "xLast" were successfully permutated to the previous permutation. The function returns false if the entries were permuted back to reverse-ordered permutation. The overloaded version uses the function "xComp" as a less than comparison function.

Header Include

#include <algorithm>

Overloads

template <class BidirIterator, class BinaryPred>
bool prev_permutation(
	BidirIterator xFirst,
	BidirIterator xLast,
	BinaryPred xComp
);

Example

#include <iostream>
#include <deque>
#include <algorithm>

int main()
{
	using namespace std;

	// Create a deque instance
	deque<int> qD;
	for (int i = 1; i <= 3; ++i) {
		qD.push_back(i);
	}

	// Call once to get the last permutation
	prev_permutation(qD.begin(), qD.end());

	deque<int>::iterator qIter;
	// Output the permutation
	cout << "Initial Perm: ";
	for (qIter = qD.begin(); qIter != qD.end(); ++qIter) {
		cout << *qIter;
	}
	cout << endl;

	// Run through each permutation backwards
	bool bHasNext = true;
	while (bHasNext) {
		bHasNext = prev_permutation(qD.begin(), qD.end());
		cout << "Permutated  : ";
		for (qIter = qD.begin(); qIter != qD.end(); ++qIter) {
			cout << *qIter;
		}
		cout << endl;
	}

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

Output

prev_permutation() Output
 

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