What is the difference between the size and the capacity of a vector?
In the context of C++ vectors, the terms "size" and "capacity" refer to different aspects of the vector.
Size: The size of a vector represents the number of elements currently stored in the vector. It indicates the actual number of elements that have been inserted into the vector.
size()
member function of the vector, like this: myVector.size()
.Capacity: The capacity of a vector represents the maximum number of elements that the vector can hold without requiring reallocation of memory. It represents the allocated storage space in the vector, which is usually greater than or equal to the size of the vector.
capacity()
member function of the vector, like this: myVector.capacity()
.In summary, the size of a vector refers to the number of elements currently stored in the vector, while the capacity represents the maximum number of elements that the vector can hold without requiring reallocation.