Skip to content Skip to sidebar Skip to footer

Cython - Iterate Through Map

I was wondering if this was possible to iterate through a map directly in Cython code, ie, in the .pyx. Here is my example: import cython cimport cython from licpp.map import map a

Solution 1:

The map iterator doesn't have elements first and second. Instead it has a operator* which returns a pair reference. In C++ you can use it->first to do this in one go, but that syntax doesn't work in Cython (and it isn't intelligent enough to decide to use -> instead of . itself in this case).

Instead you use cython.operator.dereference:

from cython.operator cimport dereference

# ...

print(dereference(it).first)

Similarly, it++ can be done with cython.operator.postincrement

Post a Comment for "Cython - Iterate Through Map"