如何编写C++版本的 Python powerset 函数
How to write C++ version of Python powerset function
我想编写用于邻接矩阵的最大 clique 算法。我正在关注一个视频,该视频解释了如何使用Python编码和实现算法。我目前正在尝试在视频中 2 分钟对电源设置函数进行编码。
def powerSet(elts):
if len(elts) == 0:
return [[]]
else:
smaller = powerSet(elts[1:])
elt = [elts[0]]
withElt = []
for s in smaller:
withElt.append(s + elt)
allofthem = smaller + withElt
return allofthem
print powerSet([1, 2, 3, 4, 5])
我想用C++重写它。我不确定我是否应该使用数组。到目前为止,我已经编写了以下内容,但我不知道如何在空数组中返回一个空数组(当elts
list
大小为 0 时)。
我为数组编写了一个isEmpty
函数,因为我不能像在 Python 中那样使用len(elts)
。我的方法可能不是最好的方法,所以我愿意接受任何建议。
更新:
array powerSet(int elts[])
{
if (isEmpty(elts)==1)
{
return {{}};
}
}
在我的 int 主中,我有:
list<int> elts;
list<int>::iterator i;
for (i=elts.begin(); i != elts.end(); ++i)
cout << *i << " ";
cout << endl;
powerSet(elts);
我不知道从这里开始做什么。
代码应该使用我们称之为"elts"(元素的缩写)的array
/list
/vector
。然后首先,它应该添加空列表[]
,然后是电源集的其余部分(全部显示在视频中)。
例如,在elts = [1,2,3,4]
的情况下,我的代码应该返回:
`[ [],[4],[3],[4,3],[2],[4,2],[3,2],[4,3,2],[1],[4,1],[3,1],[4,3,1],[2,1],[4,2,1],[3,2,1],[4,3,2,1] ] `
我不知道如何使用array
/list
/vector
来做上述事情。
这是 Python 代码的相当直译
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
//def powerSet(elts):
vector<vector<int>> powerSet(const vector<int>& elts)
{
// if len(elts) == 0:
if (elts.empty()) {
// return [[]]
return vector<vector<int>>(
1, // vector contains 1 element which is...
vector<int>()); // ...empty vector of ints
}
// else:
else {
// smaller = powerSet(elts[1:])
vector<vector<int>> smaller = powerSet(
vector<int>(elts.begin() +1, elts.end()));
// elt = [elts[0]]
int elt = elts[0]; // in Python elt is a list (of int)
// withElt = []
vector<vector<int>> withElt;
// for s in smaller:
for (const vector<int>& s: smaller) {
// withElt.append(s + elt)
withElt.push_back(s);
withElt.back().push_back(elt);
}
// allofthem = smaller + withElt
vector<vector<int>> allofthem(smaller);
allofthem.insert(allofthem.end(), withElt.begin(), withElt.end());
// return allofthem
return allofthem;
}
}
这将vector<int>
用于整数集。然后vector
这一点,即 vector<vector<int>>
是整数集的列表。你特别问了一件事
我不知道如何在空数组中返回一个空数组(当
elts
list
大小为 0 时)。
第一个 return
语句返回一个列表,其中包含一个元素,即空集,使用 vector
构造函数,其第一个参数n
是vector
中的元素数,其第二个参数是要重复n
次的元素。一个等效但更冗长的替代方案是
vector<vector<int>> powerSetOfEmptySet;
vector<int> emptySet;
powerSetOfEmptySet.push_back(emptySet);
return powerSetOfEmptySet;
我试图保持代码简单,避免太多深奥的C++功能。希望您可以在良好的参考的帮助下完成它。我使用的一个C++习语是将一个向量附加到另一个向量,如 allofthem.insert(allofthem.end(), withElt.begin(), withElt.end());
.
同样在比 Python "更接近金属"的 C++ 中,您可能只使用一个vector
代替三个向量smaller
、withElt
和 allofthem
。这导致了下面更短、更优化的代码。
//def powerSet(elts):
vector<vector<int>> powerSet(const vector<int>& elts)
{
// if len(elts) == 0:
if (elts.empty()) {
// return [[]]
return vector<vector<int>>(1, vector<int>());
}
// else:
else {
// smaller = powerSet(elts[1:])
vector<vector<int>> allofthem = powerSet(
vector<int>(elts.begin() +1, elts.end()));
// elt = [elts[0]]
int elt = elts[0]; // in Python elt is a list (of int)
// withElt = []
// for s in smaller:
// withElt.append(s + elt)
// allofthem = smaller + withElt
const int n = allofthem.size();
for (int i=0; i<n; ++i) {
const vector<int>& s = allofthem[i];
allofthem.push_back(s);
allofthem.back().push_back(elt);
}
// return allofthem
return allofthem;
}
}
测试代码如下
int main()
{
const int N = 5;
vector<int> input;
for(int i=1; i<=N; ++i) {
input.push_back(i);
}
vector<vector<int>> ps = powerSet(input);
for(const vector<int>& set:ps) {
cout << "[ ";
for(int elt: set) {
cout << elt << " ";
}
cout << "]n";
}
return 0;
}
作为脚注,我惊喜地发现从Python翻译成C++是多么简单。我读过使用Python编写(或原型)算法,然后根据需要用C++等语言重写可能是有意义的,但是,正如谚语所说,眼见为实。
这是适用于 C++98 的程序版本。我恢复了我使用的基本 C++11 功能(>>
在模板声明和基于范围的for
中)。
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
vector<vector<int> > powerSet(const vector<int>& elts)
{
if (elts.empty()) {
return vector<vector<int> >(1, vector<int>());
}
else {
vector<vector<int> > allofthem = powerSet(
vector<int>(elts.begin() +1, elts.end()));
int elt = elts[0];
const int n = allofthem.size();
for (int i=0; i<n; ++i) {
const vector<int>& s = allofthem[i];
allofthem.push_back(s);
allofthem.back().push_back(elt);
}
return allofthem;
}
}
int main()
{
const int N = 5;
vector<int> input;
for(int i=1; i<=N; ++i) {
input.push_back(i);
}
vector<vector<int> > ps = powerSet(input);
for(vector<vector<int> >::const_iterator i=ps.begin(); i!=ps.end(); ++i) {
const vector<int>& set = *i;
cout << "[ ";
for(vector<int>::const_iterator j=set.begin(); j!=set.end(); ++j) {
int elt = *j;
cout << elt << " ";
}
cout << "]n";
}
return 0;
}
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- Pybind11:将元组列表从Python传递到C++
- 如何在c++中使用引用实现类似python的行为
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 递归列出所有目录中的C++与Python与Ruby的性能
- IPC使用多个管道和分支进程来运行Python程序
- 从python中调用C++函数并获取返回值
- Python 3.7 和 excess_args 的 SWIG 问题
- Python中的for循环与C++有何不同
- 使用Pybind11向Python公开Eigen::张量
- Python str to C++ to Python str
- 如何使用Python从C++中读取谷物序列化数据
- 如何在C++中使用pybind11加载一个pickle python列表
- 如何在c++中使用system()来运行包含空格的python脚本
- python集合的C++等价物是什么.计数器
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 从python调用openMP共享库时,未定义opnMP函数
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- 如何将真正的字符串从python c-api转换为python脚本
- 如何编写C++版本的 Python powerset 函数