无法在cython中修改集合

2024-04-20 07:00:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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()

输出:

^{2}$

我不确定我是否需要deref的东西,但是没有它,类型就不匹配了。有人能解释一下我是怎么做到这一点的吗?在


Tags: from标记testdataasloadcppext
1条回答
网友
1楼 · 发布于 2024-04-20 07:00:25

AFAICT,您将python的^ {< CD1>}与C++的^ a1}合并。后者的方法是insert,而不是{}(与前者一样)。在

如果将相关行更改为:

deref(s).insert(1)

输出变成:

^{pr2}$

相关问题 更多 >