Initial capacity of vector in C++

后端 未结 6 1590
独厮守ぢ
独厮守ぢ 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条回答
  •  -上瘾入骨i
    2020-11-28 13:10

    The standard doesn't specify what the initial capacity of a container should be, so you're relying on the implementation. A common implementation will start the capacity at zero, but there's no guarantee. On the other hand there's no way to better your strategy of std::vector iv; iv.reserve(2345); so stick with it.

提交回复
热议问题