Open In App

Difference Between Size and Capacity of a Vector in C++ STL

Last Updated : 31 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to study the difference between the size and capacity of a vector in C++ STL. The size of the vector is defined as the total number of elements present in the vector. Vector is a dynamic array so it changes with the user input, unlike the static arrays which we define with the subscript operators “[]” which remain constant throughout the program and throw errors if we try to input more than the array size. Vectors can change their size and capacity according to the user input. Vectors are known as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted.

In the case of vectors:

vector<int> v;
v.size() <-- For size of the vector.
v.capacity() <-- For capacity of the vector.

Example 1: 

C++




// C++ Program to demonstrate the functionality of size
#include <iostream>
#include <vector>
using namespace std;
  
int main()
{
    vector<int> v;
    for (int i = 0; i < 9; i++)
        v.push_back(i);
    cout << v.size();
    return 0;
}


Output

9
Difference Between Size And Capacity Of A Vector In C++ STL

 

The above code and its output clearly show that the size of the vector represents the total number of elements that we store in the vector. But capacity acts a bit differently compared to size. 

Example 2: 

C++




// C++ Program to demonstrate the capacity of the vector
// which has 9 elements stored inside it.
#include <iostream>
#include <vector>
using namespace std;
  
int main()
{
    vector<int> v;
    for (int i = 0; i < 9; i++)
        v.push_back(i);
    cout << v.capacity();
    return 0;
}


Output

16
Difference Between Size And Capacity Of A Vector In C++ STL

 

You can easily observe the difference between the size and capacity of the vectors. Whenever the size of the vector becomes equal to the capacity, the vector will automatically increase its capacity and make it twice as large. 

There is a difference between size and capacity. The capacity of a vector is the total number of elements it can hold. The total amount of storage is what capacity is, but how much content (elements) are there inside the vector is what the size of a vector represents.

Example 3: 

C++




// C++ Program to demonstrate the size & capacity of the vector
#include <iostream>
#include <vector>
using namespace std;
  
int main()
{
    vector<int> v;
    for (int i = 0; i < 9; i++)
        v.push_back(i);
    cout << v.size();
    cout << endl << v.capacity();
    return 0;
}


Output

9
16
Difference Between Size And Capacity Of A Vector In C++ STL

 

The reason for this difference is the internal implementation of the vector. Unlike static arrays which are rigid in their capacity, vectors are flexible in their capacity, because vectors are dynamic in nature.

So, the size of the array is 9 because there are only 9 elements present in the vector although its capacity is 16, which means you are allowed to store up to 16 elements, and once the number of elements becomes equal to capacity it will modify itself and again increase its capacity.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads