【发布时间】:2022-01-04 22:23:06
【问题描述】:
我的目标是能够将 std::map
我在 C++ 中有这样的函数定义
void predict(std::vector<T>& seq, std::map<T, double>& probabilities)
main.cpp 看起来像这样
#include "node.h"
#include "probtree.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
PYBIND11_MAKE_OPAQUE(std::vector<double>);
PYBIND11_MAKE_OPAQUE(std::map<double, double>);
namespace py = pybind11;
using namespace std;
int main(int argc, char** argv){
}
PYBIND11_MODULE(sps_c, m){
py::bind_vector<std::vector<double>>(m, "VectorD");
py::bind_map<std::map<double, double>>(m, "MapDD");
py::class_<ProbTree<double>>(m, "ProbTree")
.def(py::init())
.def("process", &ProbTree<double>::process)
.def("predict", &ProbTree<double>::predict)
;
}
我期望的工作都没有:
>>> import sps_c
>>> pt = sps_c.ProbTree
>>> vd = sps_c.VectorD
>>> m = sps_c.MapDD
>>> pt.process(vd)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: process(): incompatible function arguments. The following argument types are supported:
1. (self: sps_c.ProbTree, arg0: sps_c.VectorD) -> None
Invoked with: <class 'sps_c.VectorD'>
也许是一个列表?
>>> pt.process([1.0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: process(): incompatible function arguments. The following argument types are supported:
1. (self: sps_c.ProbTree, arg0: sps_c.VectorD) -> None
Invoked with: [1.0]
我该如何做?
还有一个问题是 STL 方法不可用:
vd.push_back(1.0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'sps_c.VectorD' has no attribute 'push_back'
我是否必须编写一堆样板才能让 STL 容器工作?
【问题讨论】: