std::vector of std::vectors contiguity

后端 未结 4 1607
暗喜
暗喜 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:31

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

提交回复
热议问题