当前位置: 首页 > 科技观察

C++代码赏析:Map、Filter、Reduce

时间:2023-03-12 14:10:33 科技观察

的概念来自Google的论文《MapReduce: simplified data processing on large clusters》。MapReduce是一种用于大规模数据集(大于1TB)并行计算的编程模型。他们的主要思想“Map”和“Reduce”概念是从函数式编程语言中借用的,具有从向量编程语言中借用的特性。它极大地方便了程序员无需分布式并行编程就可以在分布式系统上运行他们的程序。Program=Algorithm+DataStructureAlgorithm=Control+LogicProgramComplexity=ControlComplexity(canbereduced)+LogicComplexity(theoreticallowerlimit)架构或设计的目的是把控制和逻辑从函数式编程中的map,reduce,filter分离出来,它们都是一种控制。而参数lambda就是逻辑(我们要解决的问题),它们共同组成了一个算法。最后我把数据放在数据结构中进行处理,最后就成了我们的程序。注:VegetarianC++mapstd::transformfilterstd::remove_ifreducestd::accumulateExample过滤掉奇数对上一步的计算结果进行平方,对上一步的结果求和#include#include#include#includeusingnamespacestd;intmain(){std::vectornums{1,2,3,4,5,6,7,8,9};std::vector缓存(nums.size());//过滤器自动it=std::copy_if(nums.begin(),nums.end(),cache.begin(),[](intn){returnn%2==1;});//将容器缩小到新的大小cache.resize(std::distance(cache.begin(),it));//映射std::transform(cache.begin(),cache.end(),cache.begin(),[](intn)->int{returnn*n;});autoresult=std::accumulate(cache.begin(),cache.end(),0,[](intcarry,intn){返回进位+n;});std::cout<<结果<

猜你喜欢