boost python是否支持通过ref或value返回向量的函数? [英] does boost python support a function returning a vector, by ref or value?
问题描述
我是python的新手,我看过boost python,它看起来非常 感人的.但是通过介绍我找不到 在任何示例中,对象的矢量都以python列表/元组的形式返回.
I am new to python, I have looked at boost python, and it looks very impressive. However going through the introduction I can not find any examples where, vector of objects are returned as python list/tuples.
即以这个例子为例,我想公开X类,Cont及其所有功能. 关键位将X的向量或字符串返回给python
i.e Take this example, I want to expose class X, Cont and all its functions. critical bit being return a vector of X's or strings to python
class X {};
class Cont {
.....
// how can this be exposed using boost python
const std::vector<X>& const_ref_x_vec() const { return x_vec_;}
std::vector<X> value_x_vec() const { return x_vec;}
const std::vector<std::string>& const_ref_str_vec() const { return str_vec_;}
std::vector<std::string> value_str_vec() const { return str_vec_; }
...
private:
std::vector<X> x_vec_;
std::vector<std::string> str_vec_;
};
我自己徒劳无功的尝试,试图公开类似的功能 const_ref_x_vec(),value_x_vec()等只会导致编译错误.
My own fruitless attempt at trying to expose the functions like const_ref_x_vec(), value_x_vec(),etc just leads to compile errors.
从谷歌搜索到我还没有看到任何支持返回向量的示例 按价值或参考.使用boost python甚至可能吗? 有什么解决方法吗?在这种情况下我应该使用SWIG吗?
from googling around I have not seen any example that support returning vectors by value or reference. Is this even possible with boost python? are there any workarounds ? should I be using SWIG for this case ?
任何帮助表示赞赏.
头像
推荐答案
Autopulated的原因从本质上讲是正确的,但是代码要复杂得多,而且有必要.
Autopulated's reason was essentially correct, but the code was more complicated then necessary.
vector_indexing_suite 可以为您完成所有工作:
The vector_indexing_suite can do all that work for you:
class_< std::vector<X> >("VectorOfX")
.def(vector_indexing_suite< std::vector<X> >() )
;
还有一个 map_indexing_suite .
这篇关于boost python是否支持通过ref或value返回向量的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!