gpt4 book ai didi

python - 如何在 cython 中制作从 C struct 到 int 的 C++ 映射?

转载 作者:搜寻专家 更新时间:2023-10-31 01:45:10 24 4
gpt4 key购买 nike

如何在 cython 中创建从 C struct 到 int 的 C++ 映射?

我试试下面的代码:

主要.py

#!/usr/bin/env python
# encoding: utf-8
import pyximport


pyximport.install()
from foo import Fun

Fun()

foo.pyx

#!/usr/bin/env python
# encoding: utf-8
from libcpp.map cimport map
from libcpp.string cimport string

cdef struct mystruct:
int i


def Fun():
cdef:
map[mystruct,int] dDict
mystruct A

A.i=1

dDict[A]=1

配置文件foo.pyxbld

def make_ext(modname, pyxfilename):
from distutils.extension import Extension
return Extension(name=modname,
sources=[pyxfilename],
language='C++')

运行 main.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:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\functional(143) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const __pyx_t_3foo_mystruct'
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\string(150) : see declaration of 'std::operator <'
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\functional(142) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
with
[
_Ty=__pyx_t_3foo_mystruct
]
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\map(68) : see reference to class template instantiation 'std::less<_Ty>' being compiled
with
[
_Ty=__pyx_t_3foo_mystruct
]
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xtree(22) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
with
[
_Kty=__pyx_t_3foo_mystruct,
_Ty=int,
_Pr=std::less<__pyx_t_3foo_mystruct>,
_Alloc=std::allocator<std::pair<const __pyx_t_3foo_mystruct,int>>,
_Mfl=false
]
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xtree(63) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
with
[
_Traits=std::_Tmap_traits<__pyx_t_3foo_mystruct,int,std::less<__pyx_t_3foo_mystruct>,std::allocator<std::pair<const __pyx_t_3foo_mystruct,int>>,false>
]
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE\xtree(89) : see reference to class template instantiation 'std::_Tree_ptr<_Traits>' being compiled
with
[
_Traits=std::_Tmap_traits<__pyx_t_3foo_mystruct,int,std::less<__pyx_t_3foo_mystruct>,std::al

我应该如何正确初始化和使用这个映射?预先感谢您的帮助!

最佳答案

能够使用键类型为 mystruct 的映射,您需要为 mystruct 定义一个比较运算符对象。实际上,C++ 映射是有序映射(通常由一些自动平衡树支持,例如 AVL 或红黑)。一个正确的语法是

cdef cppclass mystruct:
int i
bint lessthan "operator<"(const mystruct t) const:
return this.i < t.i

编辑:替换了i < t.i通过 this.i < t.i因为这更明确(Python之禅)。

关于python - 如何在 cython 中制作从 C struct 到 int 的 C++ 映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com