0
点赞
收藏
分享

微信扫一扫

STL next_permutation函数

松鼠树屋 2022-03-16 阅读 28

next_permutation(start,end)

功能:

使得参数范围内的容器元素按照下一个排列排序,“下一个”指按字典序升序排序;

如123的下一个:

132

再下一个:

213,

231,

312

321.

返回值:

当找不到下一个排列时返回false,否则返回true;

其参数start,end类似于sort函数中的参数,排序范围为[start,end),左闭右开,

对全范围排序时end=start+length;

该函数也适用于char和string类型:

string类型参数形式:

next_permutation(strinfo.begin(),strinfo.end());

该函数常与while,do while连用:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int i,  x[] = { 1,2,3,4 };
	do
	{
		for (i = 0; i < 4; i++)
			cout << x[i];
		cout << endl;
		
	} while (next_permutation(x,x+4));
	
	
}

结果如下:

在算法题中枚举全排列数时有可能可以用到。

 

举报

相关推荐

0 条评论