c ++ stl卷积

Ori*_*ent 11 c++ algorithm stl convolution

是否有一个很好的算法实现来计算C++ STL(甚至是boost)中两个范围的卷积?即原型(两个范围的卷积a..bc..d)的东西:

template< class Iterator >
void convolution(Iterator a, Iterator b, Iterator c, Iterator d);
Run Code Online (Sandbox Code Playgroud)

它修改了a..b范围

Mar*_*ork 2

是的 std::transform

std::transform(a, b, c, a, Op);

// a b is the the first input range
// c   is the start of the second range (which must be at least as large as (b-a)
// 
// We then use a as the output iterator as well.

// Op is a BinaryFunction
Run Code Online (Sandbox Code Playgroud)

回答评论中关于如何执行状态累积的评论:

struct Operator
{
    State& state;
    Operator(Sate& state) : state(state) {}
    Type operator()(TypeR1 const& r1Value, TypeR2 const& r2Value) const
    {
        Plop(state, r1Value, r2Value);
        return Convolute(state, r2Value, r2Value);
    }
};
State  theState  = 0;
Operator Op(theState);
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,每个 STL 算法都由某种类型的单个循环组成。因此,最有可能的是,不仅一种 STL 算法可以表达问题的解决方案,而且组合(即“std::inner_product”和“std::transform”)也可以表达问题的解决方案。 (3认同)