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为数组的长度