Sravan
Sravan

Reputation: 141

C++ Vector assignment with different type of data

In the following code piece,

#include <iostream>
#include <vector>
int main ()
{
    std::vector<float> floatVector;
    int myints[] = {1776,7,4};
    floatVector.assign (myints,myints+3);   // assigning from array.
    std::cout << "Size of floatVector: " << floatVector.size() << '\n';
    return 0;
}

A float vector is assigned with integer data from an array. Though it works perfectly, not sure if this type of assignment is correct way. Will there be any critical issues with this type of assignment ? Is there a better way ?

Upvotes: 1

Views: 142

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

It's fine.

The vector elements will each be initialised using the built-in implicit floatint conversion.

Remember: you're storing copies anyway. There's no magic pointer aliasing or anything like that.

So, this is as valid as:

const int   i = 1776;
const float f = i;

Upvotes: 1

Related Questions