SLT:
C + + STL has a function that can easily generate a full array, which is next_permutation
Look at the function declaration of Next_permutation in C + + reference:
#include <algorithm>
BOOL Next_permutation (iterator start, iterator end);
The next_permutation () function attempts to transform the given range of elements [Start,end] into the next lexicograp hically greater permutation of elements. If It succeeds, it returns true, otherwise, it returns false.
You can see from the description that the return value of Next_permutation is a Boolean type. Follow the prompts to write a standard C + + program:
1#include <iostream>2#include <algorithm>3#include <string>4 5 using namespacestd;6 7 intMain ()8 {9 stringstr;TenCIN >>str; One sort (Str.begin (), Str.end ()); Acout << str <<Endl; - while(Next_permutation (Str.begin (), Str.end ())) - { thecout << str <<Endl; - } - return 0; -}
The sort function and String.begin (), String.end () are also used, and the function declarations are as follows:
#include <algorithm>
void sort (iterator start, iterator end);
The sort function can use the complexity of NLOGN to sort the data in the parameter range.
#include <string>
Iterator begin ();
Const_iterator begin () const;
#include <string>
Iterator End ();
Const_iterator end () const;
String.begin () and String.end () can quickly access the first and trailing characters of a string.
When using big data tests, it was found that standard C + + was inefficient ... To write a C function, the efficiency has increased more than one times ...
1#include <cstdio>2#include <algorithm>3#include <cstring>4 #defineMAX 1005 6 using namespacestd;7 8 intMain ()9 {Ten intlength; One CharStr[max]; A gets (str); -Length =strlen (str); -Sort (str, str +length); the puts (str); - while(Next_permutation (str, str +length)) - { - puts (str); + } - return 0; +}
Reprint Link: https://www.slyar.com/blog/stl_next_permutation.html
Full Array (next_permutation)