python - Pybind11 类型错误

标签 python c++ vector casting pybinding

我最近一直在尝试用 C++ 编写一个函数,将 double vector 转换为字符串 vector 。我想从 python 解释器运行它,所以我使用 Pybind11 来连接 C++ 和 Python。这是我目前所拥有的,

#include <pybind11/pybind11.h>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>


std::string castToString(double v) {
    std::string str = boost::lexical_cast<std::string>(v);
    return str;
}

std::vector<std::vector<std::string> > num2StringVector(std::vector<std::vector<double> >& inputVector) {


    //using namespace boost::multiprecision;


    std::vector<std::vector<std::string> > outputVector;

    std::transform(inputVector.begin(), inputVector.end(), std::back_inserter(outputVector), [](const std::vector<double> &iv) {
        std::vector<std::string> dv;
        std::transform(iv.begin(), iv.end(), std::back_inserter(dv), &castToString);
        return dv;
    });

    return outputVector;
}


namespace py = pybind11;

PYBIND11_PLUGIN(num2String) {
    py::module m("num2String", "pybind11 example plugin");

    m.def("num2StringVector", &num2StringVector, "this converts a vector of doubles to a vector of strings.");
    m.def("castToString", &castToString, "This function casts a double to a string using Boost.");

    return m.ptr();
}

现在编译成共享库,使用命令行中的以下命令:

c++ -O3 -shared -std=gnu++11 -I ../include `python-config --cflags --ldflags --libs` num2StringVectorPyBind.cpp -o num2String.so -fPIC -lquadmath

../include 是 pybind11 include 所在的位置。编译后启动python并使用,

import num2String
values = [[10, 20], [30, 40]]
num2String.num2StringVector(values)

我收到以下错误消息“不兼容的函数参数。支持以下参数类型”。然后它给了我一个可能类型的列表,这很奇怪,因为我只是想使用一个 vector 作为我的函数参数,根据 pybind11 的文档,这是一个支持的数据类型: http://pybind11.readthedocs.io/en/latest/basics.html#supported-data-types

是否我有一个 vector 的 vector (一个二维 vector )并且不受支持?

最佳答案

您只需多加一行代码就可以完成这项工作:

#include <pybind11/stl.h>

根据 the documentation :

The following basic data types are supported out of the box (some may require an additional extension header to be included).

在包含 pybind11/STL.h 之前:

>>> import num2String
>>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Incompatible function arguments. The following argument types are supported:
    1. (std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) -> std::vector<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >

包含 pybind11/STL.h 并重新编译共享库后:

>>> import num2String
>>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
[['1', '2'], ['3', '4', '5']]

关于python - Pybind11 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37146908/

相关文章:

显示派生指标的 Python pandas pivot_table(来自两列)

python - Django 1.7 部署 AppRegistryNotReady 仅在 OpenShift 上

vector - 三.js 设置和读取相机外观矢量

c++ - 是否有使用 SSE 查找两个变量模数的好方法? (没有 SVML)

c++ - 在内部, vector 的 vector 如何扩展? C++

c++ - 在 C++ 中从 T 转换为 const T 的函数

python - 为什么numpy的效率不成比例

Python + Wand.Image - 使用连续的 pagenumber.jpg 名称将输出图像保存到 AWS

C++ 图实现

c++ - C++ 中安全高效的类型双关