algorithm下的全排列函数next_permutation

使用next_permutation,需先对要进行全排列的元素集合进行递增排序(可使用sort)。

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

void print(int a[],int n)
{
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
	cout<<endl;
}

int main()
{
	/*
	int a[6]={1,2,3,4,5,6};
	reverse(a,a+6);
	print(a,6);
	
	string str="abc";
	reverse(str.begin(),str.begin()+5);
	cout<<str;
	*/
	string str="abc";
	do
	{
		cout<<str<<endl;
	}while(next_permutation(str.begin(),str.end()));
	cout<<"***********"<<endl;
	while(1)
	{
		cout<<str<<endl;
		if(next_permutation(str.begin(),str.end())==false) break;		
	} 
	
	return 0; 
}

 

发布了180 篇原创文章 · 获赞 64 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/OpenStack_/article/details/103970948