std::vector of std::vectors contiguity

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

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

    Consider the following code:

    std::vector > vv;
    .... fill in v[0], v[1], v[2], etc
    std::vector  & 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'?

提交回复
热议问题