c – 变换和积累

c – 变换和积累,第1张

概述有没有人写过一个符合C STL的算法,它将std :: transform和std :: accumulate组合成一个单一的算法,支持一元,二元和甚至(n-ary!)变体,比如说std :: transformed_accumulate?我想要这个,因为我发现这种模式在例如线性代数中是高度可重用的,例如在(l1-)范数计算中. l1范数计算元素绝对值的总和. 嗯……我敢打赌,你可以通过将转换嵌入 有没有人写过一个符合C STL的算法,它将std :: transform和std :: accumulate组合成一个单一的算法,支持一元,二元和甚至(n-ary!)变体,比如说std :: transformed_accumulate?我想要这个,因为我发现这种模式在例如线性代数中是高度可重用的,例如在(l1-)范数计算中. l1范数计算元素绝对值的总和.解决方法 嗯……我敢打赌,你可以通过将转换嵌入二元谓词,转换元素并在转换后累积来实现.
struct times2accumulator {   int operator()( int oldvalue,int newvalue ) const {      return oldvalue + 2*newvalue;   }};int r = std::accumulate( v.begin(),v.end(),2,times2accumulator() );

该仿函数相当于:

struct times2 {   int operator()( int x ) {      return 2*x;   }};std::vector<int> tmp; tmp.reserve( v.size() );std::transform( v.begin(),std::back_inserter(tmp),times2 );int r = std::accumulate( tmp.begin(),tmp.end(),0 );

当然,这可以是通用的,只需将转换仿函数传递给通用基础仿函数:

template <typename transform>struct transform_accumulator_t {    transform t;    transform_accumulator_t( transform t ) : t(t) {}    int operator()( int oldvalue,int newvalue ) const {        return oldvalue + t(newvalue);    }};// syntactic sugar:template <typename T>transform_accumulator_t<T> transform_accumulator( T t ) {    return transform_accumulator_t<T>(t);}int r = std::accumulate(v.begin(),transform_accumulator(times2));

您还可以概括容器中的类型…甚至可以创建一个更通用的transform_accumulator,它同时接收累加器和转换函子并按顺序应用它们.实际实施留给读者练习.

总结

以上是内存溢出为你收集整理的c – 变换积累全部内容,希望文章能够帮你解决c – 变换和积累所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1237261.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存