Although we can use recursion to write the full array of programs, but since the STL already has this function why not directly use it, write a direct use of C + + STL to generate a fully sequenced program
Function Name: next_permutation
Include header file: algorithm
Function Prototypes:
Template<class bidirectionaliterator>
bool Next_permutation (bidirectionaliterator _first, Bidirectionaliterator _last );
Template<class Bidirectionaliterator, class binarypredicate>
bool Next_permutation (bidirectionaliterator _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 (default returns False if the current call arrangement reaches the maximum dictionary order, and returns true if the arrangement is dictionary), and the next arrangement is output in the order in which the dictionary is incremented. For example, in the alphabet, the next word of ABCD is arranged in ABDC)
So if you are generating a full array of arrays, first sort the pairs in ascending order, and then use the Do-while statement to loop through the Next_permutation function
1#include <iostream>2#include <algorithm>3#include <string>4 using namespacestd;5 intMain ()6 {7 stringstr;8Cin>>str;9 intlen=str.length ();Ten Char*cstr= (Char*) Str.c_str (); Onecout<<"arrange the output as follows"<<Endl; A Do - { -cout<<cstr<<Endl; the} while(Next_permutation (cstr,cstr+len)); -cout<<"after arranging, the CStr becomes:"<<Endl; -cout<<CStr; - return 0; +}
Above is a no plus sort direct call nextpermation look at the comparison of output results in case of different inputs
1#include <iostream>2#include <algorithm>3#include <string>4 using namespacestd;5 intMain ()6 {7 stringstr;8Cin>>str;9 intlen=str.length ();Ten Char*cstr= (Char*) Str.c_str (); OneSort (cstr,cstr+len); Acout<<"arrange the output as follows"<<Endl; - Do - { thecout<<cstr<<Endl; -} while(Next_permutation (cstr,cstr+len)); -cout<<"after arranging, the CStr becomes:"<<Endl; -cout<<CStr; + return 0; -}
Add a sort and look at the effect
The arrangement of C++stl