std::vector::resize() vs. std::vector::reserve()

后端 未结 6 817
梦谈多话
梦谈多话 2020-11-22 08:21

There is a thread in the comments section in this post about using std::vector::reserve() vs. std::vector::resize().

Here is the original c

6条回答
  •  情书的邮戳
    2020-11-22 08:47

    There probably should be a discussion about when both methods are called with a number that's LESS than the current size of the vector.

    Calling reserve() with a number smaller than the capacity will not affect the size or the capacity.

    Calling resize() with a number smaller than current size the container will be reduced to that size effectively destroying the excess elements.

    To sum up resize() will free up memory whereas reserve() will not.

提交回复
热议问题