0

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++?

Peter111
  • 803
  • 3
  • 11
  • 21

1 Answers1

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

CallMethodThatTakesReferenceToVector(result);

Demo

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • this doesnt seem to work. I get a compiler error, that transform can not find an overloading function. StoryTellers edit works – Peter111 Nov 14 '16 at 00:26