My function is:
void function(const float *, int sizeOfArray){...}
My vector is:
std::vector<float> myVector(size, val);
I read in the docs you can use myVector[0] as standard c++ static arrays operations.
How can I pass that vector to that function without having to copy the values to a new dynamic array? (I want to avoid using new / delete just for this).
Is it something like...?
function(myVector[0], size);
I'm using C++11 by the way.
You can use std::vector::data (since C++11) to get the pointer to the underlying array.
Returns pointer to the underlying array serving as element storage. The pointer is such that range [data(); data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).
e.g.
function(myVector.data(), myVector.size());
Is it something like...?
function(myVector[0], size);
myVector[0]
will return the element (i.e. float&
), not the address (i.e. float*
). Before C++11 you can pass &myVector[0]
.
function(myVector.data(), myVector.size());
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