简体   繁体   中英

Reading c++ 2d array in python swig

C++ part I have a class a with a public variable 2d int array b that I want to print out in python.(The way I want to access it is ab )

I have been able to wrap the most part of the code and I can call most of the functions in class a in python now.

So how can I read b in python? How to read it into an numpy array with numpy.i(I find some solution on how to work with a function not variable)? Is there a way I can read any array in the c++ library? Or I have to deal with each of the variables in the interface file.

for now b is <Swig Object of type 'int (*)[24]' at 0x02F65158> when I try to use it in python

ps: 1. If possible I don't want to modify the cpp part.

  1. I'm trying to access a variable, not a function.

So don't refer me to links that doesn't really answer my question, thanks.

Your question would be easier to answer if you could add a code snippet of your C++ definition (or C if you use int**). When you say "2d int array", do you mean a general int**? Is the array size known in advance?

At any rate, you could probably use numpy.i but it depends on the fine details of your array.

See this somewhat related question: Swig and multidimensional arrays

You'll find that passing things back and forth between languages is much easier if you use a one-dimensional array in which you access elements using, eg arr[y*WIDTH+x] .

Since you are operating in C++ you can even wrap these arrays in classes with nice operator()(int x, int y) methods for use on the C++ side.

In fact, this is the internal representation which Numpy uses for arrays: they are all one-dimensional.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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