Predicate Version of next_permutation

Viene illustrato come utilizzare la versione di del predicato next_permutation Funzione della libreria di modelli standard (STL) in Visual C++.

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

Note

[!NOTA]

La classe/nomi di parametro nel prototipo non corrisponde alla versione nel file di intestazione.alcuni sono stati modificati per migliorare la leggibilità.

next_permutation l'algoritmo modifica l'ordine degli elementi nell'intervallo [First, Last) alla permutazione e restituisce lessicografici seguenti true.Se c " è alcun next_permutation, vengono disposti sulla sequenza in modo che sia la prima permutazione e restituisce false.

[!NOTA]

next_permutation l'algoritmo presuppone che la sequenza sia ordinata in ordine crescente tramite confrontare funzione.La versione di nonpredicate utilizza confrontare funzione per ordinare le permutazioni.

Esempio

// next_permutationPV.cpp
// compile with: /EHsc
// 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.

// 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] = "K" ;
    Pattern[1] = "A" ;
    Pattern[2] = "L" ;

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

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

    // Generate all possible permutations

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

Esempio di output

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

Requisiti

intestazione: <algoritmo>

Vedere anche

Concetti

Esempi di una libreria di modelli standard