Multiply vector elements by a scalar value using STL

后端 未结 5 1088
刺人心
刺人心 2020-12-23 14:03

Hi I want to (multiply,add,etc) vector by scalar value for example myv1 * 3 , I know I can do a function with a forloop , but is there a way of doing this using

5条回答
  •  攒了一身酷
    2020-12-23 14:29

    Mordern C++ solution for your question.

    std::vector myarray;
    double myconstant{3.3};
    std::transform(myarray.begin(), myarray.end(), myarray.begin(), [&myconstant](auto& c){return c*myconstant;});
    

提交回复
热议问题