In the standard library algorithm, the Next_permutation application is widely used in the sequence operation. This function calculates the entire array of data. But how to use, how the principle, I did a simple analysis.
First look at the relevant information in the STL.
Function Prototypes:
template<class bidirectionaliterator>
bool Next_permutation (
bidirectionaliterator _first ,
bidirectionaliterator _ Last
);
template<class Bidirectionaliterator, class Binarypredicate>
BOOL Next_permutation (
bidirectionaliterator Span style= "COLOR: #009999" >_first ,
bidirectionaliterator _last ,
binarypredicate _comp
);
Two overloaded functions, the second with the predicate parameter _comp, with only two arguments in the version, and the default predicate function is "less than".
return value: type bool
Analyze the Next_permutation function execution process:
Suppose the series D1,d2,d3,d4 ...
The range is marked by [First,last], which calls next_permutation to increment the sequence by a dictionary order. For example, in the alphabet, the next word of ABCD is arranged as ABDC, but there is a key point, how to determine the next permutation in the dictionary order, not next->next->next ...
If the current call arrangement reaches the maximum dictionary order, such as DCBA, it returns false, and the arrangement is reset to the dictionary order.
A return of TRUE indicates that the next array of builds succeeded. The following focuses on this process:
The next two data are compared from the backward to the mark, if the former is less than (the default is less than) the latter, the former is X1 (position px) will be replaced, again after re-search the first one is not less than X1 data, marked as X2. Swap the X1,X2 and then set the [Px+1,last] tag range to reverse. Complete.
Important: Why this ensures that the minimum increment is obtained.
Starting from location first the data position of the original sequence and the new series is PX, and the new data is X2. [Px+1,last] is always decremented, [first,px] does not change, because x2>x1, so no matter how the X2 behind the order is larger than the original sequence, inversion [px+1,last] to make this sub-series (increment) is minimal. The new sequence is thus guaranteed to be the dictionary order of the original sequence next.
After understanding this principle, look at the following example:
int main () {
int a[] = {3,1,2};
do{
cout << a[0] << "<< a[1] <<" "<< a[2] << Endl;
}
while (Next_permutation (a,a+3));
return 0;
}
Output: 312/321 because the original sequence does not begin with the dictionary arrangement.
So to get all the full array
int a[] = {3,1,2}; Change to int a[] = {n/a};
In addition, another function in the library, Prev_permutation, is the same as next_permutation, which is the last permutation in the dictionary order from the original arrangement.
So
int main () {
int a[] = {3,2,1};
do{
cout << a[0] << "<< a[1] <<" "<< a[2] << Endl;
}
while (Prev_permutation (a,a+3));
return 0;
}
To get all the permutations of 123.
This article was reproduced in: http://blog.csdn.net/aipb2008/article/details/2227490
STL algorithm: Next_permutation anatomy