如何执行两个容器中的元素之间的两两二元操作? [英] How do I perform a pairwise binary operation between the elements of two containers?

查看:160
本文介绍了如何执行两个容器中的元素之间的两两二元操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个向量的std ::矢量< uint_32> A,B; ,我知道是同样大小的

Suppose I have two vectors std::vector<uint_32> a, b; that I know to be of the same size.

有没有C ++ 11范例做了按位与中的所有成员 A 之间 B ,并把结果的std ::矢量&lt; uint_32&GT; ℃;

Is there a C++11 paradigm for doing a bitwise-AND between all members of a and b, and putting the result in std::vector<uint_32> c;?

推荐答案

一个拉姆达应该做的伎俩:

A lambda should do the trick:

#include <algorithm>
#include <iterator>

std::transform(a.begin(), a.end(),     // first
               b.begin(),              // second
               std::back_inserter(c),  // output
               [](uint32_t n, uint32_t m) { return n & m; } ); 

更妙的是,由于@Pavel和全部C ++ 98:

Even better, thanks to @Pavel and entirely C++98:

#include <functional>

std::transform(a.begin(), a.end(), b.begin(),
               std::back_inserter(c), std::bit_and<uint32_t>());

这篇关于如何执行两个容器中的元素之间的两两二元操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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