I want to create a python wrapper for my C++ library. It would be cool, if there is a automatic conversion of std::vector to python lists and the other way round. Unfortunatly
The std_vector.i
library in SWIG provides support for std::vector
.
http://www.swig.org/Doc2.0/Library.html#Library_stl_cpp_library
You just need to tell SWIG about the template instantiations you want it to know about:
%include "std_vector.i"
namespace std {
%template(FloatVector) vector;
}
Note that the following Python code will work, but will incur an array copy:
for x in range(0, 3):
list[x] = x
myModule.myFunction(list)
To do the same thing without incurring a copy, construct the list using the SWIG-generated proxy object constructor:
list = myModule.FloatVector()
for x in range(0, 3):
list[x] = x
myModule.myFunction(list)