Open In App

Vector size vs capacity in C++ STL

Last Updated : 25 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the size of a vector is the number of elements in the vector. It can grow or shrink as elements are added or removed. To store these elements, each vector is allocated some memory. This total allocated memory is called the capacity of the vector.

The following table lists the primary differences between the vector’s size and capacity:

Parameters

Vector Size

Vector Capacity

Definition

The vector size represents actual number of elements in the vector.

The vector capacity represents maximum number of elements the vector can hold before reallocation.

Value Range

The size will always be less or equal to vector capacity.

The capacity will always be greater or equal to vector size.


It can be determined using vector size() method.

It can be determined using vector capacity() method.

Modification

The vector size changes when elements are added or removed.

It grows when the vector needs more space but does not shrink unless manually reduced.

Why Vector size and capacity are different?

Now we know that the vector size and capacity are different, but one may ask why it needs to be different? Couldn’t the amount of allocated memory be the same as the number of elements present in the vector?

The answer is NO. If we make the vector capacity can be same as that of the size of the vector, then the complexity of insertion of elements at the end will be O(n) as we may have to reallocate and copy the whole vector in each insertion. So, the capacity of the vector is generally taken larger than the size to avoid frequent reallocation to bring down the time complexity to amortized O(1).

To know more about it, refer to this article – How Does a Vector Internally Works in C++?

Example to Demonstrate the Difference

Below program demonstrate the difference between vector size() and vector capacity():

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v;

    // Both size and capacity increase on adding elements
    for (int i = 1; i <= 7; i++)
        v.push_back(i);

    cout << "Initial Size: " << v.size() << endl;
    cout << "Initial Capacity: " << v.capacity() << endl;

    // On removing elements, only size decrease
    v.pop_back();
    v.pop_back();
  
    cout << "Final Size: " << v.size() << endl;
    cout << "Final Capacity: " << v.capacity() << endl;
    return 0;
}

Output
Initial Size: 7
Initial Capacity: 8
Final Size: 5
Final Capacity: 8

Explanation: Initially both the size and capacity of vector is 0, but on adding elements both size and capacity increase. The size will increase by the number of elements added but capacity will increase double when the new size exceeds the current capacity. But on removing the elements from vector, size will decrease by the number of elements removed, but the capacity will still be same. We can decrease the capacity of vector by using vector shrink_to_fit() method to make size and capacity equal.



Next Article
Practice Tags :

Similar Reads