Microsoft KB Archive/157869

= STL Sample for Predicate Version of next_permutation Function =

Article ID: 157869

Article Last Modified on 12/8/2003

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Professional Edition

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Learning Edition 

-

<div class="notice_section">

This article was previously published under Q157869

<div class="summary_section">

SUMMARY
The sample code below illustrates how to use the predicate version of the next_permutation STL function in Visual C++.

<div class="moreinformation_section">

Prototype
template<class BidirectionalIterator, class Compare> inline bool next_permutation(BidirectionalIterator first,                        BidirectionalIterator last,                         Compare compare) NOTE: The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

Description
The next_permutation algorithm changes the order of the elements in the range [first, last) to the next lexicographic permutation and returns true. If there is no next_permutation, it arranges the sequence to be the first permutation and returns false.

NOTE: The next_permutation algorithm assumes that the sequence is sorted in ascending order using the compare function.

The non-predicate version uses the compare function to order the permutations.

Sample Code
////////////////////////////////////////////////////////////////////// // // Compile options needed: /GX // // next_permutation.cpp : Illustrates how to use the predicate version //                       of the next_permutation function. // // Functions: // //   next_permutation : Change the order of the sequence to the //                      next lexicograhic permutation. // // Written by Kalindi Sanghrajka // of Microsoft Product Support Services, // Software Core Developer Support. // Copyright (c) 1996 Microsoft Corporation. All rights reserved. //////////////////////////////////////////////////////////////////////

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

using namespace std;
 * 1) include
 * 2) include
 * 3) include
 * 4) include
 * 5) include

void main {   const int VECTOR_SIZE = 3 ;

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

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

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

StrVector Pattern(VECTOR_SIZE) ;

StrVectorIt start, end, it ;

StrOstreamIt outIt(cout, " ") ;

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

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

//Initialize vector Pattern Pattern[0] = "K" ; Pattern[1] = "A" ; Pattern[2] = "L" ;

//sort the contents of Pattern, required by next_permutation sort(start, end, less ) ;

// print content of Pattern cout << "Before calling next_permutation...\n" << "Pattern: " ; for(it = start; it != end; it++) cout << *it << " " ; cout << "\n\n" ;

// Generate all possible permutations

cout << "After calling next_permutation...." << endl ; while ( next_permutation(start, end, less ) ) {       copy(start, end, outIt) ; cout << endl ; } }

Program Output is:

Before calling next_permutation: Pattern: A K L

After calling next_permutation:. A L K K A L K L A L A K L K A

<div class="references_section">