算法将两个范围的值,并将它们放到第三个 [英] algorithm to add values of two ranges and place them into a third one

查看:110
本文介绍了算法将两个范围的值,并将它们放到第三个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道,如果有什么(在C ++ 11或升压),可以帮助我做这样的事情:

I was just wondering if there was anything (either in c++11 or boost) that could help me do something like this:

std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {2, 5, 4};
std::list<int> res;
algorithm(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(res), std::plus<int>());

的结果当然应{3,7,7}并且其中取代的std ::加上可能是任何binary_function的

the result should of course be {3, 7, 7} and where instead of std::plus could be any binary_function.

因此​​,如果任何人有一个想法,让我知道。

So if anyone has an idea, let me know.

推荐答案

您可以使用的 的std ::变换 这一点。这需要一个二元仿函数在对两个范围的元素进行操作:

You can use the 5 parameter overload of std::transform for this. This takes a binary functor to operate on pairs of elements of two ranges:

std::transform(v1.begin(), 
               v1.end(), 
               v2.begin(), 
               back_inserter(res), 
               std::plus<int>());

这篇关于算法将两个范围的值,并将它们放到第三个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆