std::vector of std::vectors contiguity

好久不见. 提交于 2019-11-28 00:42:39

No. The elements of a vector are stored in a dynamically allocated block of memory; otherwise, the capacity of the vector could not increase. The vector object just holds a pointer to that block.

The requirement that the elements be stored sequentially applies only to the elements themselves, and not to any dynamically allocated members of those elements.

To answer your final question: No. The elements of a vector of vectors are not stored contiguously.

Consider the following code:

std::vector<std::vector<int> > vv;
.... fill in v[0], v[1], v[2], etc
std::vector <int> & v = vv[1];
v.push_back (23);

If they were all stored contiguously, then this would cause each element in vv[2], vv[3], etc to move. How would that possibly work, since you're just affecting a single vector 'v'?

LihO

std::vector< std::vector<T> > is a vector of objects, that are stored in contiguous block of memory. The fact that these objects are vectors too is irrelevant though.

Although elements of the vector are stored in the contiguous block of memory, the memory where elements reside is not the part of the vector object itself.

"is it possible to access all the elements stored in such nested structure "simply" and sequentially (via a pointer or similar) the same way it can be done for a 1-D vector?"
For accessing elements of std::vector, it's better to use operator[] or at() method than retrieving the address of first element and using pointer arithmetic. For multidimensional arrays represented as vector of vectors, I suggest you to stay with operator[], which is easy to use and easy to read as well: myVector[i][j]. Worth to see vector::at vs. vector::operator[] as well :)

is it possible to access all the elements stored in such nested structure "simply" and sequentially (via a pointer or similar) the same way it can be done for a 1-D vector?

Yes, if:

  • you only ever need to add stuff to the end of your vector of vectors, and

  • you're willing to replace the vector of vectors construct with a custom data structure

What you can do then is to concatenate all those sub vectors into a single contiguous buffer, with another index buffer used for accessing this by top level entry index.

See my article here for more discussion about this, and an example 'collapsed vector vector' class implementation..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!