std::vector internals

前端 未结 3 1070
忘掉有多难
忘掉有多难 2021-01-21 05:50

How is std::vector implemented, using what data structure? When I write

void f(int n) {
  std::vector v(n);
  ...
}

Is

3条回答
  •  天涯浪人
    2021-01-21 06:34

    Your v is allocated in automatic memory. (commonly known as the stack, yes)

    The implementation details are not specified, but most commonly it's implemented using a dynamic array which is resized if you attempt to add more elements than the previous allocation can hold.

    The standard only specifies the interface (which methods it should have) and execution time boundaries.

    Since vector is a template, the implementation is visible, so locate your file and start inspecting.

提交回复
热议问题