Peter111
Peter111

Reputation: 823

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

Upvotes: 0

Views: 885

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52591

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

CallMethodThatTakesReferenceToVector(result);

Demo

Upvotes: 5

Related Questions