How is C++ std::vector implemented?

后端 未结 9 1350
不思量自难忘°
不思量自难忘° 2020-11-28 06:49

I have been using std::vector a lot, and recently I asked myself this question: \"How is std::vector implemented?\"

I had two alternatives:

9条回答
  •  悲哀的现实
    2020-11-28 07:37

    There is no one way it is implemented. Different implementations can be different, so long as the preserve the semantics and satisfy the requirements.

    At any given time, there has to be a primitive array of T to satisfy the requirements of contiguity. However, how it is allocated, grown, shrunk, and freed is up to the implementor.

    You can read the implementation for yourself, it's right there in the header file.

    I can tell you that no implementations use linked lists. They aren't consistent with the requirements of the standard.

提交回复
热议问题