Pass std :: vector as const float *?

My function:

void function(const float *, int sizeOfArray){...}

      

My vector:

std::vector<float> myVector(size, val);

      

I read in the docs that you can use myVector [0] as standard C ++ static array operations.

How can I pass this vector to this function without having to copy the values ​​into a new dynamic array ? (I want to avoid using new / delete just for this).

Is it something like ...?

function(myVector[0], size);

      

I am using C ++ 11 by the way.

+3


source to share


2 answers


You can use std :: vector :: data (since C ++ 11) to get a pointer to the underlying array.

Returns a pointer to an underlying array that serves as an item store. The pointer is such that the range [data (); data () + size ()) is always a valid range, even if the container is empty (data () in this case is not dereferenced).

eg.



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 go through &myVector[0]

.

+7


source


function(myVector.data(), myVector.size());

      



+1


source







All Articles