C++ STL next_permutation用法

next_permutation

当需要对一个序列中的元素进行全排列,可以使用该函数。
bool next_permutation(BidirectionlIterator first,BidirectionalIterator last);
包含于头文件algorithm
调用该函数即next_permutation()会取得[first,last)所标示之序列的下一个排列组合,如果没有下一个排列组合,便返回false;否则返回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;
}

在这里插入图片描述
时间复杂度较高:O(n!),n为数组的长度

发布了90 篇原创文章 · 获赞 7 · 访问量 2147

猜你喜欢

转载自blog.csdn.net/weixin_43784305/article/details/104093609