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 *。 –