python - 无法修改 cython 中的集

标签 python c++ set cython

我想在 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/

相关文章:

python - 所有可能的总和为零的整数集

python - Pandas to_dict() 将日期时间转换为时间戳

python - 原生 Python 中的 DNA 序列比对(无 biopython)

c++ - 如何在C++中将整数转换为字符

c++ - Qt : How to make a dynamically added item not comply to style sheet set at design time

c++ - std::list 的迭代器中的段错误

java - 为什么 Point 的 Set.contains() 比大小为 2 的 List<Integer> 快得多?

具有传递 __set__ 命令的 Python 数据描述符

python - 按键对python中的计数器进行排序

python - 如何在python中比较IPV6地址