next_permutation 产生全排列非常强大;
做一些暴力排序的题挺适用的。
它会对当前数组进行从小到大的全排列,
所以一般要配合sort()函数使用。
当当前序列不存在下一个排列时,函数返回false,否则返回true
例子:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num[3]={0,1,2,3};
do
{
cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
}while(next_permutation(num+1,num+4));
return 0;
}
结果是:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
而且还可以对
char,string 适用。
非常方便的一个函数。