python - Cython 中的矢量分配

标签 python pointers vector cython

这是我的cython程序

cdef struct Node:
    int v
    Node* next
    Node* pre

def f(int N):
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, 0)
    for i in xrange(N):
        narray[i] = 0

Cython编译结果:

Error compiling Cython file:
------------------------------------------------------------
...
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, 0)
    for i in xrange(N):
        narray[i] = 0
             ^
------------------------------------------------------------

testLinkList.pyx:107:14: Compiler crash in AnalyseExpressionsTransform

但我可以使用 push_back() 在向量末尾追加值,或者使用 int 代替 Node*。 怎么了?

最佳答案

您使用的是哪个版本的 Cython? 0.20.1 版适用于我的代码:

# distutils: language=c++

from libcpp.vector cimport vector

cdef struct Node:
    int v
    Node* next
    Node* pre

def f(int N):
    cdef:
        vector[Node*] narray
        int i
    narray.assign(N, NULL)
    for i in range(N):
        narray[i] = NULL

使用这个 setup.py 文件:

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("test_vector.pyx"))

关于python - Cython 中的矢量分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15071786/

相关文章:

c - 使用指向第一个参数的指针迭代函数的参数

c++ - 指针段错误问题

c++ - 释放包含多个 vector 的结构的内存

c++ - 填充 vector 的 vector 时出错:没有可调用 std::basic_string::push_back 的匹配函数

python - erlang 使用 erlport 生成 python 实例时出现 erlang 错误

python - 理解递归和多重返回

python - Ruby 字符串翻译

c++结构和方法,未正确返回

python - 类型错误 : can only concatenate tuple (not "vector") to tuple

python - 在 Linux 中检测文件打开