How do I perform a pairwise binary operation between the elements of two containers?

后端 未结 3 1353
春和景丽
春和景丽 2020-12-14 18:17

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

Is there a C++11 paradigm for doing a bitwise-AND<

3条回答
  •  渐次进展
    2020-12-14 18:59

    A lambda should do the trick:

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

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

    #include 
    
    std::transform(a.begin(), a.end(), b.begin(),
                   std::back_inserter(c), std::bit_and());
    

提交回复
热议问题