如何在python中从C结构体到int进行c++映射
How to make a C++ mapping from C struct to int in cython?
如何在cython中从C结构到int进行c++映射?
我尝试下面的代码:
main.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.0VCINCLUDExlocale(342) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEfunctional(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.0VCINCLUDEstring(150) : see declaration of 'std::operator <'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEfunctional(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.0VCINCLUDEmap(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.0VCINCLUDExtree(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.0VCINCLUDExtree(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.0VCINCLUDExtree(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
Edit:用this.i < t.i
替换i < t.i
,因为这更显式(Python的Zen)。
相关文章:
- 使用 switch,但我想用字符串而不是 int 进行选择
- 如何对无符号长 int 进行位掩码?
- 按值对 std::unordered_map<std:::string, std::atomic<unsigned int>> 进行排序
- 如何仅根据第二个元素对map<int,pair<int,int> >进行排序?
- 解析时间戳char到INT进行计算
- 如何使用向量<int>进行超大的整数乘法
- 将值pixel与int进行比较
- 如何对向量<对<字符串、对<int、int>>>进行排序?
- 如何对向量< 对< int 、对<int 、<对字符串、对<int 、int > > > > >进行排序?
- <int>按键值总和对映射<列表<列表>,int> 进行排序
- 为什么模板试图用'int&'而不是"int"进行实例化?
- C++字符串与 int 进行比较
- 将long-long与int进行比较时为什么会溢出
- 将空字符串与 int 进行比较
- 按值的降序对 C++ 中的 Map<string,int> 进行排序
- C++使用(void*)强制转换为int进行比较
- 使用 int&& 参数通过 int&& 进行函数初始化
- 在constexpr函数内部对int进行突变
- STL 排序不能很好地对我的向量<对<int,int> >进行排序
- 如何在python中从C结构体到int进行c++映射