繁体   English   中英

痛饮:传递矢量 <float> 从c ++到python

[英]Swig: Pass a vector<float> from c++ to python

我想用C ++编写一些将向量返回给python的代码。 我尝试了以下示例,但它返回以下对象。

<Swig Object of type 'std::vector< float > *' at 0x100331f90>

如何将其转换为列表,以便可以在python中使用它?

我的代码:

/* File: example.i */
%module example

%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}

std::vector<float> Test(int n);

/* File: example.cpp */

#include <vector>

std::vector<float> Test(int n){

    std::vector<float> a(4);
    a[1] =  1;
    a[2] = 24234;
    return a;
}

/* File: example.h 
http://www.swig.org/Doc1.3/Python.html#Python_nn4
*/

#include <vector>
std::vector<float> Test(int n);

我找到了这个问题的答案。 很难找到适合此问题的正确关键字。

解决方案在这里说明。

%include <std_vector.i> //Takes care of vector<type>

仍然缺少以下三行

namespace std
{
    %template(FloatVector) vector<float>;
}

总的来说,我的example.i现在看起来像这样:

/* File: example.i */
%module example
%include "std_vector.i"

%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}

namespace std
{
  %template(FloatVector) vector<float>;
}


std::vector<float> Test(int n);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM