我找到的最好的东西是这样的:example。不幸的是,代码崩溃了,我似乎不明白为什么。我用的是:
template<typename T>
void python_to_vector(boost::python::object o, vector<T>* v) {
try {
object iter_obj = object(handle<>(PyObject_GetIter(o.ptr())));
return;
for (;;) {
object obj = extract<object>(iter_obj.attr("next")());
// Should launch an exception if it cannot extract T
v->emplace_back(extract<T>(obj));
}
} catch(error_already_set) {
PyErr_Clear();
// If there is an exception (no iterator, extract failed or end of the
// list reached), clear it and exit the function
return;
}
}
找到一个解决我问题的迭代器:
假设有一个函数需要
std::vector<Foo>
最简单的方法是将
vector
公开给python。所以现在在python中,我们可以将
Foo
s粘贴到vector
中,并将向量传递给bar
相关问题 更多 >
编程相关推荐