ILi*_*cos 5 c++ python std python-itertools
我需要将一个用 Python 编写的代码段移植到 C++ 中,但该代码段在 python 中使用了 itertools 的组合。
我真正有兴趣移植到 C++ 的那一行是:
for k in combinations(range(n-i),2*i):
Run Code Online (Sandbox Code Playgroud)
range(n-i) 在 Python 中将生成一个列表 0 to (n-i) - 1
让 n = 16, i = 5
print range(n-i)
输出:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
和 python 组合将生成该列表中的所有可能组合。
例如
print list(combinations(range(n-i),2*i))
输出:
[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 10), (0, 1, 2, 3, 4, 5, 6, 7, 9, 10), (0, 1, 2, 3, 4, 5, 6, 8, 9, 10), (0, 1, 2, 3, 4, 5, 7, 8, 9, 10), (0, 1, 2, 3, 4, 6, 7, 8, 9, 10), (0, 1, 2, 3, 5, 6, 7, 8, 9, 10), (0, 1, 2, 4, 5, 6, 7, 8, 9, 10), (0, 1, 3, 4, 5, 6, 7, 8, 9, 10), (0, 2, 3, 4, 5, 6, 7, 8, 9, 10), (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
我想使用std::vector和next_permutation从 C++生成类似的输出,但我仍然得到错误的结果。这是我目前的方法:
for(int j = 0; j < n-i; j++) {
temp_vector.push_back(j);
}
Run Code Online (Sandbox Code Playgroud)
该代码段相当于range(n-i)在 Python 中。
但是下面的片段:
do {
myvector.push_back(temp_vector);
} while(next_permutation(temp_vector.begin(),temp_vector.begin()+2*i));
cout<<myvector.size()<<endl;
Run Code Online (Sandbox Code Playgroud)
不等同combinations(range(n-i),2*i))于 Python,我尝试了许多变体,但仍然无法得出我期望的结果。
例如:
让 n = 16 i = 5
Python
>>> print len(list(combinations(range(n-i),2*i)))
11
C++
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> temp_vector;
vector< vector<int> > myvector;
int n = 16, i = 5;
for(int j = 0; j < n - i; j++) {
temp_vector.push_back(j);
}
do {
myvector.push_back(temp_vector);
} while(next_permutation(temp_vector.begin(), temp_vector.begin()+2*i));
cout<<myvector.size()<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
g++ combinations.cpp
./a.out
3628800
任何指导将不胜感激!非常感谢!
组合和排列不是一回事。
组合是另一个集合中的项目子集的无序列表。排列是列表中项目的唯一顺序。
您将从 11 项内容的列表中生成 10 项内容的所有组合,因此您将得到 11 个结果,每个结果都缺少原始 11 项中的不同一项。
生成每个排列都会生成原始 11 个项目的每个唯一顺序。由于本例中的项目都是唯一的,这意味着结果将是 11!列出其中每个包含全部 11 项的列表。然而,您仅从前 10 个项目生成排列,因此您得到了 10 个!列表,其中都不包含第 11 项。
您需要找到一种生成组合而不是排列的算法。
没有内置的组合算法。std::next_permutation 可以用作生成组合的算法的一部分:请参阅在 c++ 中生成组合。
这是组合算法的旧提案草案,包括代码。