How can I write C++ code to print variable values in python prompt with SWIG -


i new swig , wrap c++ classes , use in python. practice, writing vector class (similar 1 in stl). want print out elements of vector after entering vector name in python environment.

it like:

>>>v 1 2 3 4 

how can achieve that?

you can use %template directive , std_vector.i library in .i file.

reference: http://www.swig.org/doc3.0/swigdocumentation.html#library_std_vector

mymodule.h file (c++ header file)

#include <vector>  std::vector<int> getvectorint(); 

mymodule.cpp file (c++ source file)

std::vector<int> getvectorint() {     std::vector<int> voutput;      for(int = 0; < 10; i++)         voutput.push_back(i);      return voutput; } 

mymodule.i file (swig interface file)

%module mymodule %{ #include "mymodule.h" %}  %include "std_vector.i"  %template(intvector) std::vector<int>;  %include "mymodule.h" 

python output

>>> my_vector = mymodule.getvectorint() >>> my_vector (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -