共用方式為


Predicate Version of next_permutation

說明如何使用述詞的版本的 next_permutation Visual C++ 標準樣板程式庫 (STL) 函式。

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

備註

注意事項注意事項

在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。

next_permutation演算法,以變更該範圍中的項目順序 [First, Last) 的下一步的 lexicographic 排列,並傳回 ,則為 true。 如果沒有任何next_permutation,它將成為第一個排列的順序排列,並傳回 ,則為 false

注意事項注意事項

next_permutation演算法會假設順序遞增排序順序使用比較函式。Nonpredicate 版本使用比較的順序排列的函式。

範例

// 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 ;
    }
}

範例輸出

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

需求

標頭: <algorithm>

請參閱

概念

標準樣板程式庫範例