C++实现全排列(字典序)

以前都是写用复杂的逻辑语言实现全排列,直到发现了C++中原来已经封装好了一个全排列函数:next_permutation(s.begin(),s.end()),头文件

这个函数会自动生成下一个字典序的排序,对应的还有prev_permutation,生成上一个字典序排序,炒鸡的简单明了,省去一大堆复杂的循环~~;

代码如下:

#include 
#include 
#include
#include
#include 
using namespace std;  
int main()  
{ 
	int n;
	cin >> n;
	while(n--)
	{
	    string str;
		cin >> str;  
		sort(str.begin(),str.end());
		do
		{
			cout << str << endl;
		}while(next_permutation(str.begin(),str.end()));
	}
	 return 0;  
} 


你可能感兴趣的:(自己的一些练习)