0
4

2 回答 2

4

As std::vector guarantees the data is stored in contiguous memory, you can convert a vector to a pointer like so:

std::vector<float> myFloats;
void *ptr = static_cast<void*>(&myFloats[0]); // or &myFloats.front()

Edit: if you are writing to it without calling push_back, make sure you resize enough space first!

于 2011-06-18T00:57:18.263 回答
4

Given a std::vector<float>, you can obtain a pointer to the contiguous buffer of floats thus:

std::vector<float> v;
fillMyVectorSomehow(v);

float* buffer = &v[0]; // <---

You could cast this to void* and pass it through.

于 2011-06-18T00:57:41.930 回答