以下程序缺少一個置換條目。std :: next_permutation缺少一個條目
#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
另一種方法是使用適當的數學公式計算應該有多少置換,然後只是多次迭代,而忽略來自next_permutation的返回值。 – 2011-12-27 06:56:34
如果您知道它已經分類,則不應該對容器進行排序。 – wilhelmtell 2011-12-27 06:56:51
@KarlKnechtel由於整數溢出,這可能不是微不足道的。 – wilhelmtell 2011-12-27 06:58:16