Initial capacity of vector in C++

后端 未结 6 1593
独厮守ぢ
独厮守ぢ 2020-11-28 12:47

What is the capacity() of an std::vector which is created using the default constuctor? I know that the size() is zero. Can we state t

6条回答
  •  迷失自我
    2020-11-28 13:17

    As a slight addition to the other answers, I found that when running under debug conditions with Visual Studio a default constructed vector will still allocate on the heap even though the capacity starts at zero.

    Specifically if _ITERATOR_DEBUG_LEVEL != 0 then vector will allocate some space to help with iterator checking.

    https://docs.microsoft.com/en-gb/cpp/standard-library/iterator-debug-level

    I just found this slightly annoying since I was using a custom allocator at the time and was not expecting the extra allocation.

提交回复
热议问题