【问题标题】:Wrapping method returning c++ std::array<std::string, 4> in cython在 cython 中返回 c++ std::array<std::string, 4> 的包装方法
【发布时间】:2016-09-01 13:19:34
【问题描述】:

我的方法在 C++ 代码中返回 std::array&lt;std::string, 4&gt;。我使用 Cython 包装此代码。我尝试使用内存视图包装数组。但结果是Invalid base type for memoryview slice: string。那么我可以包装我的std::array&lt;std::string, 4&gt; 以在 python 中使用它,比如 strs 列表吗?

【问题讨论】:

    标签: python c++ arrays string cython


    【解决方案1】:

    最简单的方法可能就是复制到 Python 列表中。

    为了回答这个问题,我假设您已将数组包装为类似于 this answer 并将其命名为 arrstr4。然后代码看起来像:

    def f():
        cdef arrstr4 res = your_cplus_plus_function()
        py_res = []
        for i in range(4):
            py_res.append(res[i]) # take advantage of autoconversion to python string
        return py_res
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 2012-06-09
      • 2021-06-30
      • 1970-01-01
      • 2016-07-21
      • 2019-08-17
      • 2023-03-31
      相关资源
      最近更新 更多