Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
abs
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

prev_permutation (STL Samples) 

Illustrates how to use the prev_permutation Standard Template Library (STL) function in Visual C++.


template<class BidirectionalIterator> inline
   bool prev_permutation(
      BidirectionalIterator First,
      BidirectionalIterator Last
   )

NoteNote

The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

The prev_permutation algorithm changes the order of the elements the range [First, Last), to the previous lexicographic permutation and returns true. If there is no prev_permutation, it arranges the sequence to be the first permutation and returns false.

NoteNote

The prev_permutation algorithm assumes the sequence is sorted in descending order using operator<. The nonpredicate version uses the operator< to order the permutations.

// prev_permutation.cpp
// compile with: /EHsc
// Illustrates how to use the prev_permutation
// function.
//
// Functions:
//    prev_permutation : Change the order of the sequence to the
//                       previous lexicographic permutation.

// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable: 4786)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std ;

int main()
{
    const int VECTOR_SIZE = 3;

    // Define a template class vector of strings
    typedef vector<string> StrVector;

    // Define an iterator for template class vector of strings
    typedef StrVector::iterator StrVectorIt;

    // Define an ostream iterator for strings
    typedef ostream_iterator<string>
    StrOstreamIt;

    StrVector Pattern(VECTOR_SIZE);

    StrVectorIt start, end, it;

    StrOstreamIt outIt(cout, " ");

    // location of first element of Pattern
    start = Pattern.begin();

    // one past the location last element of Pattern
    end = Pattern.end();

    //Initialize vector Pattern
    Pattern[0] = "C";
    Pattern[1] = "B";
    Pattern[2] = "A";

    // print content of Pattern
    cout << "Before calling prev_permutation..." << endl << "Pattern: [";
    for (it = start; it != end; it++)
        cout << " " << *it;
    cout << " ]" << endl;

    // Generate all possible permutations
    cout << "After calling prev_permutation...." << endl;
    while ( prev_permutation(start, end) )
    {
        cout << "[ ";
        copy(start, end, outIt);
        cout << "]" << endl;
    }
}

Before calling prev_permutation...
Pattern: [ C B A ]
After calling prev_permutation....
[ C A B ]
[ B C A ]
[ B A C ]
[ A C B ]
[ A B C ]

Header: <algorithm>

Community Additions

ADD
Show:
© 2015 Microsoft