I have a pointer to the first element of a double array: double* p
.
I also know the length of the array: n
.
Is there a way I can copy this into a std::vector
? Preferably using a constructor and allocator. I'd like to do something like
std::vector<double> v = std::vector(p, n);
but can't figure out which constructor is closest to this. I'm using C++11.
std::vector<>
has a constructor that takes a range. You can use it like this:
std::vector<double> v(p, p + n);
Try
std::vector<double> v( p, p + n );
Or if the vector was already defined then
v.assign( p, p + n );
If you want to append the vector then
v.insert( v.end(), p, p + n );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With