次のプログラムには1つの置換エントリがありません。それは一つのエントリが欠落している理由std :: next_permutationに1つのエントリがありません
#include <iostream>
#include <vector>
#include <algorithm>
int main (int argc, char **argv) {
std::vector<int> temp;
temp.push_back(10);
temp.push_back(2);
temp.push_back(4);
temp.push_back(4);
do {
std::copy(temp.begin(),temp.end(),std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
}while (std::next_permutation (temp.begin(), temp.end()));
}
は、以下のプログラム
10 2 4 4
10 4 2 4
10 4 4 2
の出力である
2 4 4 10
もう1つの方法は、適切な数式を使用する必要がある置換の数を計算してから、それを何度も繰り返し、next_permutationからの戻り値を無視することです。 –
既にソートされていれば、コンテナをソートしないでください。 – wilhelmtell
@ KarlKnechtel整数のオーバーフローのため、おそらくそれほど簡単ではありません。 – wilhelmtell