我有以下资料:
# distutils: language = c++
from libcpp.vector cimport vector
def foo():
cdef vector[vector[int]] matrix
matrix.reserve(100);
matrix[0] = [1,2,3]
print(matrix)
foo()
它打印出一个空列表;我希望它打印出第一个元素为[1,2,3]
的向量
Setup.py脚本:
from setuptools import setup
from Cython.Build import cythonize
setup(
name='myfile',
ext_modules=cythonize("my_file.pyx"),
zip_safe=False,
)
我该如何解决这个问题
您应该使用
matrix.resize(100)
而不是matrix.reserve(100)
std::vector<>::reserve
不会更改向量的大小-它只保留内存,因此不需要进一步(多次)重新分配:现在
matrix[0] = [1,2,3]
是未定义的行为(大小为0
-没有元素),不幸的是,它没有由于超出范围的访问而崩溃相关问题 更多 >
编程相关推荐