我想在 C++ 中声明一组无符号整数,并在 Python 中修改它:
%load_ext Cython
%load_ext cythonmagic
%%cython
# distutils: language = c++
from libcpp.set cimport set as cpp_set
from cython.operator cimport dereference as deref
def modify_test_data():
cdef (cpp_set[int])* s = new cpp_set[int]()
print deref(s), type(deref(s))
deref(s).add(1)
print deref(s)
modify_test_data()
输出:
set([]) <type 'set'>
set([]) # here i would expect 'set([1])'
我不确定我是否需要 deref 的东西,但如果没有它,类型就不匹配。有人可以解释一下我如何以干净/优雅的方式做到这一点吗?
最佳答案
AFAICT,您将 Python 的 set
与 C++ 的 std::set
混为一谈。 。后者的方法是 insert
,而不是 add
(与前者相同)。
如果将相关行更改为:
deref(s).insert(1)
输出变为:
set([]) <type 'set'>
set([1])
关于python - 无法修改 cython 中的集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483303/