如何在Cython中声明指针向量?

Tar*_*ula 5 c++ python cython

我想宣布这样的事情:

cdef vector[Node*] list2node(list my_list):
Run Code Online (Sandbox Code Playgroud)

但是Cython给了我这个错误:

cdef vector[Node*] list2node(list my_list):
                ^
------------------------------------------------------------

mod.pyx:77:20: Expected an identifier or literal
Run Code Online (Sandbox Code Playgroud)

sam*_*ias 6

您不应该需要*- vector[Node]应该为Node指针的向量生成代码.使用Cython 0.14.1:

cdef class Node: 
    pass
cdef vector[Node] list2node():
    pass
cdef vector[int] test_int():
    pass
cdef vector[int*] test_intp(): 
    pass
Run Code Online (Sandbox Code Playgroud)

生成C++代码:

static PyTypeObject *__pyx_ptype_3foo_Node = 0;
static std::vector<struct __pyx_obj_3foo_Node *> __pyx_f_3foo_list2node(void);
static std::vector<int> __pyx_f_3foo_test_int(void);
static std::vector<int *> __pyx_f_3foo_test_intp(void);
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你,这非常直观. (2认同)