size vs capacity of a vector?

后端 未结 6 1646
刺人心
刺人心 2020-11-27 03:10

I am a bit confused about this both of these look same to me. Although it may happen that capacity and size may differ on different compilers. how it may differ. Its also sa

6条回答
  •  情深已故
    2020-11-27 03:46

    Let's say you have a bucket. At most, this bucket can hold 5 gallons of water, so its capacity is 5 gallons. It may have any amount of water between 0 and 5, inclusive. The amount of water currently in the bucket is, in vector terms, its size. So if this bucket is half filled, it has a size of 2.5 gallons.

    If you try to add more water to a bucket and it would overflow, you need to find a bigger bucket. So you get a bucket with a larger capacity and dump the old bucket's contents into the new one, then add the new water.

    Capacity: Maximum amount of stuff the Vector/bucket can hold. Size: Amount of stuff currently in the Vector/bucket.

提交回复
热议问题