prev_permutation (STL Samples)
說明如何使用 prev_permutation Visual C++ 標準樣板程式庫 (STL) 函式。
template<class BidirectionalIterator> inline
bool prev_permutation(
BidirectionalIterator First,
BidirectionalIterator Last
)
備註
注意事項 |
---|
在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。 |
prev_permutation演算法的範圍變更的元素順序 [First, Last)、 先前的 lexicographic 排列,並傳回 ,則為 true。 如果沒有任何prev_permutation,它將成為第一個排列的順序排列,並傳回 ,則為 false。
注意事項 |
---|
prev_permutation演算法會假設順序按遞減順序使用運算子 <。Nonpredicate 版本使用運算子 < ,下令排列方式。 |
範例
// 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;
}
}
Output
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 ]
需求
標頭: <algorithm>