用于将矢量返回类型的锯齿状C ++向量转换为Python列表的SWIG类型映射是什么?
std::vector<std::vector<int>>
答案 0 :(得分:3)
在绑定.i文件中,输入以下typemap:
%typemap(out) std::vector<std::vector<int>>&
{
for(int i = 0; i < $1->size(); ++i)
{
int subLength = $1->data()[i].size();
npy_intp dims[] = { subLength };
PyObject* temp = PyArray_SimpleNewFromData(1, dims, NPY_INT, $1->data()[i].data());
$result = SWIG_Python_AppendOutput($result, temp);
}
}
答案 1 :(得分:1)
SWIG中有内置支持,但它返回一个元组而不是列表。但是,这可能就足够了:
%module test
%{
#include <vector>
%}
%include <std_vector.i> // built-in support
%template() std::vector<int>; // declare instances of templates used to SWIG.
%template() std::vector<std::vector<int> >;
%inline %{ // Example code.
std::vector<std::vector<int> > func()
{
std::vector<std::vector<int> > vv;
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
vv.push_back(v);
v.clear();
v.push_back(4);
v.push_back(5);
vv.push_back(v);
return vv;
}
%}
结果:
>>> import test
>>> test.func()
((1, 2, 3), (4, 5))