0
我用用Cython來包裝我的C++類:用Cython映射C++數據結構
Foo.h
class Foo
{
private:
std::map<int,int > myMap;
std::vector<int> myVector;
public:
// An example of internal structures initialization
Foo()
{
for (int i=0; i<10; i++)
{
myVector.push_back(i);
myMap[i]=i*i;
}
}
std::map<int,int> getMyMap()
{
return myMap;
}
std::vector<int> getMyVector()
{
return myVector;
}
}
我不知道是否有某種方式來獲得std::map
爲蟒蛇dict
和STD :: vector作爲python list
而不顯式創建(並因此浪費內存)這些結構的副本。
甲temptative實施在std ::矢量如下:
cdef extern from "Foo.h":
cdef cppclass Foo:
Foo()
vector[int] getMyVector()
cdef class pyFoo:
cdef Foo *thisptr # hold a C++ instance which we're wrapping
def __cinit__(self):
self.thisptr = new Foo()
def getMyVector(self):
cdef vector[int] aa
cdef int N
b= []
aa = self.thisptr.getMyVector()
N=aa.size()
for i in range(N):
b.append(aa[i])
return b;
但這顯然必須存儲包含相同數據的兩個結構。我想知道是否有一種方法從C++映射到列表與cython或者我應該使用boost :: python?
好吧,我唯一遇到的問題是處理矢量的自定義類型。在這種情況下,我們使用'vector',但cython編譯器無法應付,例如'vector ',其中'Foo'是我的一個類。 –
linello
事實上,你所要求的python list和dict應該是最適合的。至於Foo * - 您可以通過使用uintptr_t類型而不是Foo *來解決此問題,並在cdef方法中將其轉換回Foo *。 –