STL next_permutation函数(字典序升序排列,好东西哦!)

做洛谷看题解发现大佬用这个函数做的,感觉很强,学了一下,好像发现了新大陆,这是什么神仙函数啊!!!Orz

这个函数的作用就是把数组里的数按下一个字典序进行排序

举个例子吧,比如 a[3]={1,2,3};

排一次之后变{1,3,2}

再排一次变{2,1,3}

再排一次变{2,3,1}

写成程序就是这样的

#include 
#include 
using namespace std;
int main(){
	int n=3,a[10],j=0,i;
	for(i=0;i<3;i++){
		scanf("%d",&a[i]);
	}
	next_permutation(a,a+n);
	for(i=0;i<3;i++){
		printf("%d ",a[i]);
	}
	return 0;
}

 

运行结果

STL next_permutation函数(字典序升序排列,好东西哦!)_第1张图片

 

然后我试了试字符串,发现也能排,代码差不多,直接贴结果了

STL next_permutation函数(字典序升序排列,好东西哦!)_第2张图片

 

 然后我又试了试二维字符串,竟然还能排,我的天呐

#include 
#include 
using namespace std;
int main(){
	int n,i;
	char a[10][5];
	scanf("%d",&n);
	getchar();
	for(i=0;i

这是结果

 STL next_permutation函数(字典序升序排列,好东西哦!)_第3张图片

就和写题最常用的一样,一位一位往下比较进行排序

还有一个prev_permutation函数,作用是上一个字典序排序,用法相同

好东西是要和大家分享的,试验完后立即写了这篇博客,太强了我服了

 

你可能感兴趣的:(STL)