When should we use reserve() of vector?

后端 未结 4 2002
甜味超标
甜味超标 2021-01-06 03:47

I always use resize() because I cannot use reserve as it gives error: vector subscript out of range. As I\'ve read info about the differences of resize() and reserve(), I sa

4条回答
  •  星月不相逢
    2021-01-06 04:45

    resize(n) allocates the memory for n objects and default-initializes them.

    reserve() allocates the memory but does not initialize. Hence, reserve won't change the value returned by size(), but it will change the result of capacity().

提交回复
热议问题