I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStlAlgorithmsMin Max

prev_permutation

Permutes the order of the element
+ View version details

Key Facts

Gyroscopic Couple: The rate of change of angular momentum (\inline \tau) = \inline I\omega\Omega (In the limit).
  • \inline I = Moment of Inertia.
  • \inline \omega = Angular velocity
  • \inline \Omega = Angular velocity of precession.


Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.

Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.

Definition

The prev_permutation() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class BidirectionalIterator >
   bool prev_permutation(
      BidirectionalIterator first, 
      BidirectionalIterator last
   );
template < class BidirectionalIterator, class BinaryPredicate >
   bool prev_permutation(
      BidirectionalIterator first, 
      BidirectionalIterator last,
      BinaryPredicate comp
   );

Parameters:
Parameter Description
first A bidirectional iterator pointing to the position of the first element in the range to be permuted
last A bidirectional iterator pointing to the position one past the final element in the range to be permuted
comp User-defined predicate function object that defines the comparison criterion to be satisfied by successive elements in the ordering. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied

Description

Prev_permutation function reorders the range [first, last) into the previous permutation from the set of all permutations that are lexicographically ordered.

The first version uses operator< for comparison and the second uses the function object comp.

Return Value

Returns true if there was a previous permutation and has replaced the original ordering of the range, false otherwise.

Complexity

The complexity is linear; performs at most (last - first) / 2 swaps.
Example:
Example - prev_permutation algorithm
Problem
The following code prints all six permutations of the string "abc" in reverse order using prev_permutation function.
Workings
#include <algorithm>
#include <string>
#include <iostream>
#include <functional>
int main()
{
    std::string s="abc";
    std::sort(s.begin(), s.end(), std::greater<char>());
    do {
        std::cout << s << ' ';
    } while(std::prev_permutation(s.begin(), s.end()));
    std::cout << '\n';
}
Solution
Output:

cba cab bca bac acb abc
References