C ++ STL next_permutation usage

next_permutation

When it is desired for a whole series of elements are arranged, the function may be used.
bool next_permutation (BidirectionlIterator first, BidirectionalIterator last );
included in the header algorithm
calls the function i.e. next_permutation () will get marked the [first, last) sequence next permutations , if there is no next permutations will return false; otherwise, it returns true.

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
 int a[4] = { 1,2,3,4 };
 do
 {
  for (int i = 0; i < 4; ++i)
   cout << a[i] << ' ';
  cout << endl;
 } while (next_permutation(a, a + 4));
 return 0;
}

Here Insert Picture Description
High time complexity: O (! N), n is the length of the array

Published 90 original articles · won praise 7 · views 2147

Guess you like

Origin blog.csdn.net/weixin_43784305/article/details/104093609