element-wise multiplication of two vectors in c++

后端 未结 2 1443
悲&欢浪女
悲&欢浪女 2021-02-07 11:21

I am trying to do the following mathematical operation with two vectors:

v1 = [a1][a2][a3][a4][a5]
v2 = [b1][b2][b3][b4]b5]

Want to compute:

相关标签:
2条回答
  • 2021-02-07 11:46

    You could look into std::valarray. It's designed to allow mathematical operations on every element in the array.

    0 讨论(0)
  • 2021-02-07 11:53
    std::transform( v1.begin()+1, v1.end(),
                    v2.begin()+1, v.begin(),  // assumes v1,v2 of same size > 1, 
                                              //       v one element smaller
                    std::multiplies<int>() ); // assumes values are 'int'
    

    You can replace v.begin() with std::back_inserter(v) if v is empty, you should reserve() memory upfront to avoid multiple allocations.

    0 讨论(0)
提交回复
热议问题