2016-05-10 6 views
4

私は最近、ダブルスのベクトルを文字列のベクトルに変換するC++で関数を記述しようとしていました。私はこれをPythonインタプリタから実行したいので、Pybind11を使ってC++とPythonをインターフェースしています。これはPybind11タイプのエラー

#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(); 
} 

は今、これはコマンドラインから次のコマンドを使用して、共有ライブラリにコンパイルし、私がこれまで持っているものです。

あるpybind11を含む場合に../includeがある
c++ -O3 -shared -std=gnu++11 -I ../include `python-config --cflags --ldflags --libs` num2StringVectorPyBind.cpp -o num2String.so -fPIC -lquadmath 

。コンパイル後のpythonと使用、

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

を起動し、私は次のエラー、取得「互換性のない関数の引数を次の引数の型がサポートされています」。 http://pybind11.readthedocs.io/en/latest/basics.html#supported-data-types

はそれは:それはその後、私の可能なタイプのリストを与える場合は、私はちょうどpybind11のドキュメントによると、これはサポートされるデータ型である私の関数の引数としてベクトルを使用しようとしているので、これは奇妙です私はベクトルのベクトル(2次元ベクトル)を持っており、それはサポートされていませんか?

答えて

6

あなたは、コードのちょうど1つの余分ラインでこの作業を行うことができます:the documentation 1として

#include <pybind11/stl.h> 

次の基本的なデータ型は、箱から出してサポートされています(一部はANが必要な場合があります追加の拡張ヘッダーが含まれます)。 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']] 
関連する問題