【问题标题】:Cython C++ and std::stringCython C++ 和 std::string
【发布时间】:2010-10-06 08:42:07
【问题描述】:

从 cython 使用 C++ 标准 std::string 的最佳方式是什么?无论如何,最后一个 cython 发行版应该很容易,但我想知道为什么有 std::vector 的包装器而不是 std::string 的包装器......

【问题讨论】:

    标签: c++ python cython


    【解决方案1】:

    Cython 0.16 includes wrappers for std::string,可以通过以下方式导入:

    from libcpp.string cimport string
    

    【讨论】:

    • 似乎我在 OSX 上使用 Cython 0.16 的声明不符合 Linux 上的 Cython 0.17,使用 OSX 我得到Cannot convert 'string' to Python object
    【解决方案2】:

    哎呀,这个问题已经在这里挂了几天了。最后我这样做了:

    cdef extern from "string" namespace "std":
        cdef cppclass string:
            char* c_str()
    

    这不是一个完整的解决方案,但它仍然可以解决问题。

    【讨论】:

    • 我也会添加 string(char *) 。这使您可以在 python 字符串中前进和后退。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多