使用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;
}


本文介绍如何利用C++标准库中的next_permutation函数来实现字符串或数组的全排列。在使用next_permutation之前,需要对元素进行递增排序。通过示例代码展示了next_permutation函数的使用方法,包括如何打印所有可能的排列。
666

被折叠的 条评论
为什么被折叠?



