Why Vector's size() and capacity() is different after push_back()

前端 未结 6 730
鱼传尺愫
鱼传尺愫 2021-01-03 00:09

I just starting to learning vectors and little confused about size() and capacity() I know little about both of them. But why in this program both

6条回答
  •  -上瘾入骨i
    2021-01-03 00:31

    I think the following question can give you more detail about the capacity of a vector.

    About Vectors growth

    I will reference the answer in above question.

    The growth strategy of capacity is required to meet the amortized constant time requirement for the push_back operation. Then, the strategy is designed to have a exponential growth generally when the space is lack. In short, the size of vector indicate the number of elements now, while the captacity show its ability used to push_back in future.

提交回复
热议问题