从今天开始记录一下自己的学习过程,有什么错误欢迎大家指出,也欢迎大家和我交流讨论。
在数学中我们常用到排列组合,而在C++中有很多排列方法,其中next_permutation()函数就是全排列函数。下面介绍一下用法:
首先要引用库函数:
#include
函数原型是:
bool next_permutation(iterator start,iterator end)
当全排列到头的时候返回false,否则返回true。常用于while循环的条件中,代码示例:
#include#include using namespace std; int main(){ int a[4]={1,2,3,4}; int num=0;//计数 do{ for(int i=0;i<4;i++){ cout< 结果如下:
1 2 3 4 1 2 4 3 1 3 2 4 1 3 4 2 1 4 2 3 1 4 3 2 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 2 4 1 3 2 4 3 1 3 1 2 4 3 1 4 2 3 2 1 4 3 2 4 1 3 4 1 2 3 4 2 1 4 1 2 3 4 1 3 2 4 2 1 3 4 2 3 1 4 3 1 2 4 3 2 1值得注意的是当数组a[4]改变成[2,3,1,4]的时候,结果会变化:
2 3 1 4 2 3 4 1 2 4 1 3 2 4 3 1 3 1 2 4 3 1 4 2 3 2 1 4 3 2 4 1 3 4 1 2 3 4 2 1 4 1 2 3 4 1 3 2 4 2 1 3 4 2 3 1 4 3 1 2 4 3 2 1因为next_permutation()函数是从当前排列往后开始的,对应的也有往前开始的prev_permutation()函数,和next_permutation()用法差不多。



