algorithm - STL C++

next_permutation()

Declaration

template <class BidirIterator>
bool next_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 next non-ordered permutation. The function returns false if the entries were permuted back to being ordered. The overloaded version uses the function "xComp" as a comparison function.

Header Include

#include <algorithm>

Overloads

template <class BidirIterator, class BinaryPred>
bool next_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);
	}

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

	bool bHasNext = true;
	// Run through each permutation
	while (bHasNext) {
		bHasNext = next_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

next_permutation() Output
 

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