开发者

How to access to memory block which allocated by std::vector?

开发者 https://www.devze.com 2023-02-19 17:38 出处:网络
Many people recommend vector class for variable length array. And I have to pass pointer to memory block to G开发者_开发知识库L. How can I access pointer to memory block allocated by std::vector? Use

Many people recommend vector class for variable length array. And I have to pass pointer to memory block to G开发者_开发知识库L. How can I access pointer to memory block allocated by std::vector?


Use the address of first element. If your vector is v then &v[0] will work.


ContainerType* pData  = &vec.front();


std::vector<int> v(1000);

int *p = &v[0];

//treat p as if it points to an array of 1000 ints
//all 1000 ints default-initialized with 0!
0

精彩评论

暂无评论...
验证码 换一张
取 消