prev_permutation和next_permutation使用区别

1.我们先看看next_permutation这个泛型算法

#include <iostream>
#include <algorithm>
using namespace std;

int main ()
{
int a[] = {1,2,3};
do
{
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
}while(next_permutation(a,a+3));

return 0;
}

输出结果是

1 2 3 
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

当a[]={2,1,3}时

输出结果是

2 1 3
2 3 1
3 1 2
3 2 1

可以知道这个排序时按照字典序来排序的;

同样对于prev_permutation

#include <iostream>
#include <algorithm>
using namespace std;

int main () 
{
	int a[] = {3,2,1};
	do
	{
		cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
	}while(prev_permutation(a,a+3));
	
	return 0;
}

  输出结果如下;

3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3

个排序就是与字典序有关

 

 

你可能感兴趣的:(ext)