next_permutation関数の使用
next_permutation、中国語は「次の順列」を意味し、その名前と同じように、次の順列を見つけるために使用されます。next_permutation()は、[first、last)でマークされたシーケンスの次の順列と組み合わせを取得します。次の順列と組み合わせがない場合はfalseを返し、それ以外の場合はtrueを返します。この関数は、ヘッダーファイル「#include」にインクルードされています。
next_permutation(首指针,尾指针);
例:文字列が与えられた場合、この文字列の辞書式順列を見つけます
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
sort(str.begin(),str.end());
do{
cout<<str<<endl;
}while(next_permutation(str.begin(),str.end()));
return 0;
}
注:この例は、並べ替えで並べ替える必要があります。並べ替えない場合は、配置の一部のみが出力されます。結果は次のとおりです。
string str="bca";
do{
cout<<str<<endl;
}while(next_permutation(str.begin(),str.end()));