Apo*_*tus 5 c++ python cython missing-data ordered-map
我想要一个分步指南,如何在 cython 中使用 unordered_map。
我已经从https://gist.github.com/ikuyamada/3265267将文件 unordered_map.pxd 包含到 Cython/Includes/libcpp 中,并使用了其他 3 个文件:
主要.py:
import pyximport;
pyximport.install()
from foo import F
F()
Run Code Online (Sandbox Code Playgroud)
foo.pyx:
from libcpp.unordered_map cimport unordered_map
def F():
cdef unordered_map[int, int] my_map
my_map[1]=11
my_map[2]=12
print my_map[1],my_map[2]
Run Code Online (Sandbox Code Playgroud)
foo.pyxbld: (将 foo.pyx 编译成 C++)
def make_ext(modname, pyxfilename):
from distutils.extension import Extension
return Extension(name=modname,
sources=[pyxfilename],
language='C++')
Run Code Online (Sandbox Code Playgroud)
当我运行 test.py 时,出现错误:
foo.cpp
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
C:\Users\kitov\.pyxbld\temp.win-amd64-2.7\Release\pyrex\foo.cpp(316) : fatal error C1083: Cannot open include file: 'tr1/unordered_map': No such file or directory
Run Code Online (Sandbox Code Playgroud)
我使用 Win7,Python 2.7 64 位,VS2008 Professional。
我已经解决了这个问题:
1)我需要从http://www.microsoft.com/en-us/download/details.aspx?id=6922安装Visual C++ 2008功能包
2)我需要使用以下unordered_map.pxd:
from libcpp.utility cimport pair
cdef extern from "<unordered_map>" namespace "std::tr1":
cdef cppclass unordered_map[T, U]:
cppclass iterator:
pair[T, U]& operator*() nogil
iterator operator++() nogil
iterator operator--() nogil
bint operator==(iterator) nogil
bint operator!=(iterator) nogil
unordered_map()
unordered_map(unordered_map&)
U& operator[](T&) nogil
# unordered_map& operator=(unordered_map&)
U& at(T&) nogil
iterator begin() nogil
void clear() nogil
size_t count(T&) nogil
bint empty() nogil
iterator end() nogil
pair[iterator, iterator] equal_range(T&) nogil
void erase(iterator) nogil
void erase(iterator, iterator) nogil
size_t erase(T&) nogil
iterator find(T&) nogil
pair[iterator, bint] insert(pair[T, U]) nogil
iterator insert(iterator, pair[T, U]) nogil
void insert(input_iterator, input_iterator)
size_t max_size() nogil
void rehash(size_t)
size_t size() nogil
void swap(unordered_map&) nogil
Run Code Online (Sandbox Code Playgroud)
与https://gist.github.com/ikuyamada/3265267相比,更改了一行:
cdef extern from "<tr1/unordered_map>" namespace "std::tr1":
Run Code Online (Sandbox Code Playgroud)
->
cdef extern from "<unordered_map>" namespace "std::tr1":
Run Code Online (Sandbox Code Playgroud)