gpt4 book ai didi

python - Cython 中的矢量分配

转载 作者:太空狗 更新时间:2023-10-30 00:15:03 25 4
gpt4 key购买 nike

这是我的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com