What does std::vector look like in memory?

前端 未结 6 693
耶瑟儿~
耶瑟儿~ 2021-02-01 13:10

I read that std::vector should be contiguous. My understanding is, that its elements should be stored together, not spread out across the memory. I have simply acce

6条回答
  •  天涯浪人
    2021-02-01 13:39

    std::vector being a contiguous container means exactly what you think it means.

    However, many operations on a vector can re-locate that entire piece of memory.

    One common case is when you add element to it, the vector must grow, it can re-allocate and copy all elements to another contiguous piece of memory.

提交回复
热议问题