Python swig,从列表到非标准向量
我想设置typemap,这样我们就可以传递Python列表,而不是非标准向量 在C++中,我有< /P>Python swig,从列表到非标准向量,python,c++,casting,swig,Python,C++,Casting,Swig,我想设置typemap,这样我们就可以传递Python列表,而不是非标准向量 在C++中,我有< /P> template<typename T> class mm_vector { void set_mm_vector(const mm_vector * copy); } 我有以下错误: File "...", line 1295, in set_mm_vector def set_mm_vector(self, *args): return _pyamt.vec_of_ints
template<typename T>
class mm_vector
{
void set_mm_vector(const mm_vector * copy);
}
我有以下错误:
File "...", line 1295, in set_mm_vector
def set_mm_vector(self, *args): return _pyamt.vec_of_ints_set_mm_vector(self, *args)
ValueError: Expecting a list
如果有任何建议,我将不胜感激 SWIG内置了对向量和模板的支持,因此您不必从头开始实现它。下面是一个简短的例子:
%module vec
// Include the built-in support for std::vector
%include <std_vector.i>
// Tell SWIG about the templates you will use.
%template() std::vector<int>;
// %inline adds the following code to the wrapper and exposes its interface via SWIG.
%inline %{
class Test {
std::vector<int> m_v;
public:
void set(const std::vector<int>& v) { m_v = v;}
std::vector<int> get() const {return m_v;}
};
%}
File "...", line 1295, in set_mm_vector
def set_mm_vector(self, *args): return _pyamt.vec_of_ints_set_mm_vector(self, *args)
ValueError: Expecting a list
%module vec
// Include the built-in support for std::vector
%include <std_vector.i>
// Tell SWIG about the templates you will use.
%template() std::vector<int>;
// %inline adds the following code to the wrapper and exposes its interface via SWIG.
%inline %{
class Test {
std::vector<int> m_v;
public:
void set(const std::vector<int>& v) { m_v = v;}
std::vector<int> get() const {return m_v;}
};
%}
>>> import vec
>>> t=vec.Test()
>>> t.set([1,2,3])
>>> t.get()
(1, 2, 3)