Peter111 Peter111 - 21 days ago 7
C++ Question

C++ Create a std::vector<float> const& object

I have one vector::

static const std::vector<float> vector1


and one reference of a vector:

std::vector<float> const& vector2


I need to multiply the values of these vectors with each other and store them in a new vector.

For example:

result[0]=vector1[0]*vector2[0]
result[1]=vector1[1]*vector2[1]


Then I need to give this vector to a method which only accepts:

std::vector<float> const& result


How do I do this in C++?

Answer
std::vector<float> result;
std::transform(
    vector1.begin(), vector1.end(),
    vector2.begin(),
    std::back_inserter(result), std::multiplies<float>());

CallMethodThatTakesReferenceToVector(result);

Demo

Comments