Steven Lu Steven Lu - 11 months ago 37
C++ Question

Assigning a vector from an array (pointer)

Is there a standard way to accomplish this that is better than a


If I had an array type supposedly I can do this:

double d_array[] = { 1.0, 2.0, 3.0 };
std::vector<double> d_vector(d_array, d_array+3);

But I can't do this when I only have a
double *
and an
indicating its length.

Edit: Actually, I think I actually can do this. The error messages are quite a handful, though, if you get your type parameters wrong (which is why it didn't work for me at first).

ken ken
Answer Source

Of course you can do it the same way

int length;
double *d;
//allocate memory and data to pointer
std::vector<double> d_vector(d, d+length);