Declaration
template <class BidirectionalIterator> bool next_permutation(BidirectionalIterator first, BidirectionalIterator last); template <class BidirectionalIterator, class Compare> bool next_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp); template <class BidirectionalIterator> bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last); template <class BidirectionalIterator, class Compare> bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last, Compare comp);
Description The set of all permuations of the range [ first, last) is assumed to be lexicographically sorted with respect to operator< or comp.
The next_permutation function creates the next permutation of the elements in the range [ first, last) into the range [ first, last) , returning true (ie. a non-zero value) if this new permutation is lexicographically greater than the sequence that was previously in the range [ first, last) , and false otherwise (with the resulting sequence being the one that is in sorted order).
The prev_permutation function creates the previous permutation of the elements in the range [ first, last) into the range [ first, last) , returning true (ie. a non-zero value) if this new permutation is lexicographically less than the sequence that was previously in the range [ first, last) , and false otherwise (with the resulting seuence being the one that is the reverse of the sorted order sequence).
Type requirements
Group Permutation generators (mutator)
Time complexity Linear.
The number of swaps required is at most half the size of the range [ first, last) .
Space complexity Constant.
Mutative? Yes.
Example Select here to view the source code for the example.
Implementation Select here to view source code (Copyright (C) 1994, Hewlett-Packard Company) for this algorithm.