2

特定の文字リスト、この場合は「eta」のすべての順列を見つけようとしています

std::string s="eta";
do
{
    std::cout<<s<<std::endl;

}while(std::next_permutation(s.begin(),s.end()));

次の出力が得られます。

eta
tae
tea

しかし、私が1つのことを変えると

std::string s="eta";

std::string s="aet";

出力は次のようになります

aet
ate
eat
eta
tae
tea

これは、私が期待する順列の正しい数です。「スクランブルされた」文字列がアルファベット順になると、明らかに何か違うことが起こるのでしょうか?

または、このあいまいさの原因は何ですか?

4

3 に答える 3

8

next_permutationすべての順列のソート順で、シーケンスを次の順列に変更します。したがって、辞書順の最初ではない順列から開始すると、next_permutation が false を返す前に、すべての順列の一部しか得られません。

于 2013-01-03T14:50:22.227 に答える
3

izomorphiusが指摘したように、次の順列は最後に進んだ順列で停止します。したがって、それらすべてが必要な場合は、単にsort文字リストです。

于 2013-01-03T14:52:47.860 に答える
1

next_permutation rearranges the elements in the range [first, last) into the lexicographically next greater permutation of elements,

so there's nothing wrong with your output :)

于 2013-01-03T14:52:09.383 に答える