std::vector of std::vectors contiguity

后端 未结 4 1606
暗喜
暗喜 2020-12-01 15:56

I know that std::vector internally stores it\'s data contiguously (unless it is std::vector) both in the old C++03

相关标签:
4条回答
  • 2020-12-01 16:22

    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..

    0 讨论(0)
  • 2020-12-01 16:31

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

    0 讨论(0)
  • 2020-12-01 16:33

    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.

    0 讨论(0)
  • 2020-12-01 16:47

    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'?

    0 讨论(0)
提交回复
热议问题