相关疑难解决方法(0)

如何使用SWIG在python中扩展模板化的c ++类以允许[]运算符

我有一个模板化的c ++数组类,它使用标准的vector类:

#include <vector>
#include <string>

using namespace std;

template<typename T>
class Array1D{
private:
    vector<T> data_; 
    int xsize_; 
public:
    Array1D(): xsize_(0) {};

    // creates vector of size nx and sets each element to t
    Array1D(const int& nx, const T& t): xsize_(nx) {
        data_.resize(xsize_, t);
    }

    T& operator()(int i) {return data_[i];}
    T& operator[](int i) {return data_[i];}
};
Run Code Online (Sandbox Code Playgroud)

我的SWIG界面文件看起来像

%module test

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

%include "std_vector.i"

// Array 1D Typemaps
// typemaps for standard vector<double>
namespace std{
%template(DoubleVector) vector<double>; …
Run Code Online (Sandbox Code Playgroud)

c++ python swig

6
推荐指数
2
解决办法
1751
查看次数

标签 统计

c++ ×1

python ×1

swig ×1