创建C++;stl正向迭代器使用Boost::Python给定numpy数组 创建一个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; } } 模板 void vec\u fill\u递增(\u InputIt begin,\u InputIt end,typename std::iterator\u traits::value\u type start,typename std::iterator\u traits::value\u type increment) { typedef typename std::迭代器\特征::值\类型\类型; if(std::is_floating_point::value) { 标准::大小\u t计数=0; while(开始!=结束) { *开始=开始+(_类型)计数*增量); ++开始; ++计数; } } 否则{ while(开始!=结束) { *开始=开始; 开始+=增量; ++开始; } }

创建C++;stl正向迭代器使用Boost::Python给定numpy数组 创建一个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; } } 模板 void vec\u fill\u递增(\u InputIt begin,\u InputIt end,typename std::iterator\u traits::value\u type start,typename std::iterator\u traits::value\u type increment) { typedef typename std::迭代器\特征::值\类型\类型; if(std::is_floating_point::value) { 标准::大小\u t计数=0; while(开始!=结束) { *开始=开始+(_类型)计数*增量); ++开始; ++计数; } } 否则{ while(开始!=结束) { *开始=开始; 开始+=增量; ++开始; } },python,c++,numpy,boost,boost-python,Python,C++,Numpy,Boost,Boost Python,Boost Python中已经有一个stl输入迭代器的实现(即stl_iterator.hpp),但它是不可变的,所以这对我没有帮助,所以我尝试基于输入迭代器创建自己的stl_forward_迭代器 namespace boost { namespace python { // An STL forward iterator over a python sequence template<typename ValueT>

Boost Python中已经有一个stl输入迭代器的实现(即stl_iterator.hpp),但它是不可变的,所以这对我没有帮助,所以我尝试基于输入迭代器创建自己的stl_forward_迭代器

namespace boost {
    namespace python
    {
        // An STL forward iterator over a python sequence
        template<typename ValueT>
        struct stl_forward_iterator
        : boost::iterator_facade<
            stl_forward_iterator<ValueT>
            , ValueT
            , std::forward_iterator_tag
        >
        {
            stl_forward_iterator()
            : impl_()
            {
            }

            // ob is the python sequence
            stl_forward_iterator(boost::python::object const &ob)
            : impl_(ob)
            {
            }

        private:
            friend class boost::iterator_core_access;

            void increment()
            {
                this->impl_.increment();
            }

            ValueT& dereference() const
            {
                return extract<ValueT&>(this->impl_.current().get())();
            }

            bool equal(stl_forward_iterator<ValueT> const &that) const
            {
                return this->impl_.equal(that.impl_);
            }

            objects::stl_input_iterator_impl impl_;
        };
    }
}
namespace boost{
名称空间python
{
//python序列上的STL前向迭代器
模板
struct stl_forward_迭代器
:boost::迭代器<
前向迭代器
,ValueT
,std::前向迭代器标记
>
{
stl_前向迭代器()
:impl_()
{
}
//ob是python序列
stl_forward_迭代器(boost::python::object const&ob)
:impl_u2;(ob)
{
}
私人:
朋友类boost::迭代器\u核心\u访问;
无效增量()
{
此->执行增量();
}
ValueT&取消引用()常量
{
返回extract(this->impl.current().get())();
}
bool equal(stl_forward_iterator const&that)const
{
返回this->impl\u.equal(that.impl\u);
}
对象::stl\u输入\u迭代器\u impl impl;
};
}
}
我真正做的唯一更改是将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();
}
模板
空包装填充增加(boost::python::numeric::array&array,T start,T increment){
boost::python::stl_forward_迭代器begin(数组);
boost::python::stl_forward_迭代器end;
矢量填充增量(开始、结束、开始、增量);
}
BOOST_PYTHON_模块(示例){
导入数组();
p::numeric::array::set_module_和_type(“numpy”、“ndarray”);
p::def(“填充增加”和“包裹填充增加”);
}
int main(int argc,字符**argv)
{
PyImport_AppendInittab(“示例”&PyInit_示例);
Py_初始化();
皮伦·欧·斯普勒斯特林(
“导入示例\n”
“导入numpy\n”
z3=numpy.zeros((1024,),dtype=numpy.int)\n
“示例.填充增加(z3,0,1)\n”
“打印(z3)\n”
);
Py_Finalize();
}
但我得到了一个错误:

< P> <强> TypeError:没有注册转换器能够从Python类型的Nypy.int 32 < /强>

中提取C++引用到int类型。 它发生在我的stl_forward_迭代器中的解引用函数上,我不知道如何解决这个问题

如果在这种情况下不存在可以帮助的东西,我真的认为应该有一个建议,除非有不同的解决方案