创建一个Python前向迭代器,使NUPY数组作为C++函数的输入有困难,它将STL开始和结束迭代器作为模板,并修改它,这里是C++函数的代码:
template<typename _InputIt>
void vec_fill_increasing(_InputIt begin, _InputIt end, typename std::iterator_traits<_InputIt>::value_type start, typename std::iterator_traits<_InputIt>::value_type increment)
{
typedef typename std::iterator_traits<_InputIt>::value_type _Type;
if (std::is_floating_point<_Type>::value)
{
std::size_t count = 0;
while (begin != end)
{
*begin = start + ((_Type)count*increment);
++begin;
++count;
}
}
else {
while (begin != end)
{
*begin = start;
start += increment;
++begin;
}
}
在boostpython中已经有一个stl输入迭代器的实现(即stl_迭代器.hpp)但它不是可变的,所以在这里这对我没有帮助,所以我尝试基于输入迭代器创建自己的stl_forward_迭代器。在
^{pr2}$我真正做的唯一的改变是将deference函数的返回改为引用,而不仅仅是一个值。在
我在我的主要应用程序中使用了以下内容:
template<typename T>
void wrap_fill_increasing(boost::python::numeric::array& array, T start, T increment) {
boost::python::stl_forward_iterator<T> begin(array);
boost::python::stl_forward_iterator<T> end;
vec_fill_increasing(begin, end, start, increment);
}
BOOST_PYTHON_MODULE(example) {
import_array();
p::numeric::array::set_module_and_type("numpy", "ndarray");
p::def("fill_increasing", &wrap_fill_increasing<int>);
}
int main(int argc, char **argv)
{
PyImport_AppendInittab("example", &PyInit_example);
Py_Initialize();
PyRun_SimpleString(
"import example\n"
"import numpy\n"
"z3 = numpy.zeros((1024,), dtype=numpy.int)\n"
"example.fill_increasing(z3, 0, 1)\n"
"print(z3)\n"
);
Py_Finalize();
}
但我得到了一个错误:
<强> TypeError:没有注册转换器能够从类型的Python对象中提取C++引用到int类型数字.int32
我不知道如何在函数中迭代并解决这个问题。在
如果在这种情况下没有什么可以帮助的,我真的认为应该有一个作为建议,除非有不同的解决方案。在
如有任何帮助,将不胜感激,谢谢。在
目前没有回答
相关问题 更多 >
编程相关推荐