Cython should automatically convert a C ++ set to a python set when necessary, however, if you really need to use iterators on a C ++ object, you can also do this.
If we make a very simple example, when we build a set in C ++
libset.cc
#include <set> std::set<int> make_set() { return {1,2,3,4}; }
libset.h
#include <set> std::set<int> make_set();
Then we can write a cython shell for this code, where I gave an example of how to iterate through a set in a good pythonic way (which uses C ++ iterators in the background) and an example of how to do it directly using iterators.
pyset.pyx
from libcpp.set cimport set from cython.operator cimport dereference as deref, preincrement as inc cdef extern from "libset.h": cdef set[int] _make_set "make_set"() def make_set(): cdef set[int] cpp_set = _make_set() for i in cpp_set:
This can then be compiled with a simple setup.py
setup.py
from distutils.core import setup, Extension from Cython.Build import cythonize setup( ext_modules = cythonize(Extension( "pyset", sources=["pyset.pyx", "libset.cc"], extra_compile_args=["-std=c++11"], language="c++" )))
source share