c++;stl卷积 在C++ STL(甚至Boost)中,有两种算法计算强的算法有很好的实现吗? i、 e.具有原型的东西(两个范围的卷积a..b和c..d): template 无效卷积(迭代器a、迭代器b、迭代器c、迭代器d);
它修改c++;stl卷积 在C++ STL(甚至Boost)中,有两种算法计算强的算法有很好的实现吗? i、 e.具有原型的东西(两个范围的卷积a..b和c..d): template 无效卷积(迭代器a、迭代器b、迭代器c、迭代器d);,c++,algorithm,stl,convolution,C++,Algorithm,Stl,Convolution,它修改a..b范围是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 BinaryFun
a..b
范围是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
要回答有关如何在注释中执行状态累加的注释,请执行以下操作:
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);
我不太确定从两个序列到这两个序列中的一个的“卷积”应该是什么:这似乎与我的理解不同。下面是使用可变数量迭代器的版本。因为我现在实在是太懒了,所以我将使用一个不太常见的概念,将目标迭代器作为第一个参数传递,而不是作为最后一个参数传递。下面是相应的zip()
算法的实现:
#include <tuple>
namespace algo
{
template <typename... T>
void dummy(T...)
{
}
template <typename To, typename InIt, typename... It>
To zip(To to, InIt it, InIt end, It... its)
{
for (; it != end; ++it, ++to) {
*to = std::make_tuple(*it, *its...);
algo::dummy(++its...);
}
return to;
}
}
#包括
名称空间算法
{
模板
无效虚拟(T…)
{
}
模板
压缩(到,初始化它,初始化结束,它…它)
{
for(;it!=end;++it,++to){
*to=std::make_tuple(*it,*its…);
algo::dummy(++its…);
}
返回;
}
}
下面是一个简单的测试程序,我用它来验证上述操作是否符合我的预期:
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
enum class e { a = 'a', b = 'b', c = 'c' };
std::ostream& operator<< (std::ostream& out,
std::tuple<int, double, e> const& v)
{
return out << "["
<< std::get<0>(v) << ", "
<< std::get<1>(v) << ", "
<< char(std::get<2>(v)) << "]";
}
int main()
{
typedef std::tuple<int, double, e> tuple;
std::vector<int> v{ 1, 2, 3 };
std::deque<double> d{ 1.1, 2.2, 3.3 };
std::list<e> l{ e::a, e::b, e::c };
std::vector<tuple> r;
algo::zip(std::back_inserter(r), v.begin(), v.end(), d.begin(), l.begin());
std::copy(r.begin(), r.end(),
std::ostream_iterator<tuple>(std::cout, "\n"));
}
#包括
#包括
#包括
#包括
#包括
枚举类e{a='a',b='b',c='c'};
std::ostream&operator编写一个添加到第三个范围(最初填充为零)的函数并不困难。我不知道,假设这是一个离散的卷积,其中的范围是相同的长度,写这并不难,它与变换非常相似。事实上,您甚至可以使用transform来实现这一点,其中第三个参数是反向迭代器。在我看来,每个STL算法都由某种类型的单个循环组成。因此,最有可能的是,不只是一个STL算法表达了问题的解决方案,而是组合(即std::inner_product
和std::transform
)可以。我不知道Op
如何累积所有O(n^2)没有内部构建大型临时容器的产品术语。@j_random_hacker:这很容易使Op成为函子。@LokiAstari:但是您是否同意示例中的State
成员需要包含两个临时容器——一个容器保存到目前为止看到的第一个序列中的所有元素,第二个序列的所有元素都有另一个Plop()
需要将传递到operator()
中的两个新元素中的每一个添加到这些内部容器中,而covolate()
将需要循环(这可以使用std::internal_product()
)来构建一个乘积总和。最后,需要反转其中一个迭代器范围。如果调用zip(OutIt、InIt、InItPack…),并且InItPack包含参数对(而不是std::pair),则通过多一点模板代码,还可以确保可变传递迭代器的长度匹配。然而,它大约有150行代码,这主要是由于对助手函数免费使用了递归模板。
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
enum class e { a = 'a', b = 'b', c = 'c' };
std::ostream& operator<< (std::ostream& out,
std::tuple<int, double, e> const& v)
{
return out << "["
<< std::get<0>(v) << ", "
<< std::get<1>(v) << ", "
<< char(std::get<2>(v)) << "]";
}
int main()
{
typedef std::tuple<int, double, e> tuple;
std::vector<int> v{ 1, 2, 3 };
std::deque<double> d{ 1.1, 2.2, 3.3 };
std::list<e> l{ e::a, e::b, e::c };
std::vector<tuple> r;
algo::zip(std::back_inserter(r), v.begin(), v.end(), d.begin(), l.begin());
std::copy(r.begin(), r.end(),
std::ostream_iterator<tuple>(std::cout, "\n"));
}