How to access to memory block which allocated by std::vector?
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!
精彩评论