且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++ void指针

更新时间:2022-06-27 00:50:28

As std :: vector 保证数据存储在连续的内存中,可以将向量转换为如下的指针:

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()

编辑:如果您正在写入, code> push_back ,请确保 resize 先有足够的空间!

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