What is the vector::shrink_to_fit() function in C++?

In this shot, we will learn about the vector::shrink_to_fit() function in C++.

Introduction

Before understanding this function, let’s first understand two important terms:

  • Capacity: This refers to the amount of memory allocated to the vector container and which can be used at a particular time. It means that if the particular memory is allocated to the vector, whether it has that number of elements or not, it counts to its capacity.
  • Size: This refers to the actual number of elements present in the vector, which is actually holding some data or elements into it.

How do vectors increase/decrease their sizes?

When we use the push_back() function on a vector, it may need to increase its capacity.

For example, if we initialize an empty vector and then try to call push_back() and push one element, then as the vector’s current size and capacity are zero, it will increase its capacity to 11 and then insert that element into it. If we try to insert one element again, it will double its capacity to 22 and then insert the new element. Similarly, if we want to insert one more element, it will double its capacity to 44 and insert the third element. Now, suppose we again want to insert any new element. In that case, as it already has a capacity of 44, it will not increase its capacity yet and insert that fourth element into the vector, and this process goes on.

So, we conclude that if the vector exceeds its capacity, it will double its capacity.

Now let’s understand the vector::shrink_to_fit() function. As the name suggests, this function asks the vector container to reduce its capacity to its size. In other words, the capacity will become equal to the number of elements actually present inside the vector container.

Syntax

The syntax of the vector::shrink_to_fit() function is given below:

void shrink_to_fit();

Parameter

vector::shrink_to_fit() does not accept any parameter.

Return value

vector::shrink_to_fit() does not return any value.

Code

Let’s have a look at the code now.

#include <iostream>
#include <vector>
using namespace std;
void print_vector(vector<int> vec){
for(int x: vec)
cout << x << " ";
}
int main()
{
vector<int> vec;
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(6);
vec.push_back(2);
cout << "Original vector: ";
print_vector(vec);
cout << endl;
cout << "Capacity of vector is: " << vec.capacity()<<endl;
vec.resize(3);
cout << "Vector contents after reducing the size to 3 are: ";
print_vector(vec);
cout << endl;
cout << "Capacity of vector is: " << vec.capacity()<<endl;
vec.shrink_to_fit();
cout << "Capacity of vector after using shrink_to_fit() fuction is: " << vec.capacity();
return 0;
}

Explanation

  • In line 1, we included the C++ standard header file for input output stream (iostream) which is used to read and write from streams.

  • In line 2, we included the header file for the C++ standard vector, which includes all the functions and operations related to the vector container.

  • In line 3, we used standard (std) namespace, which means we use all the things within the std namespace.

  • From lines 5 to 8, we created a simple function that accepts a vector and prints its elements.

  • In line 12, we declared the vector, which will contain integer type elements, and named it as vec.

  • From lines 13 to 17, we pushed back the different elements to the vector container vec.

  • In line 20, we printed the original vector.

  • In line 22, we printed the current capacity of the vector vec using the capacity() function. We can see in the output that its capacity is 88, and that can be understood by the explanation given above at the starting.

  • In line 24, we resized the vector vec to size 33.

  • In line 28, we again printed the current capacity of the vector vec using the capacity() function. We can see in the output that its capacity is still 88 despite resizing it.

  • In line 30, we called the function shrink_to_fit(), which results in changing the capacity of the vector container to its size.

  • In line 31, we again printed the current capacity of the vector vec using the capacity() function, and this time, the capacity of the vector vec has changed from 88 to 33.

So, in this way, we can use the vector::shrink_to_fit() function to shrink the capacity of the vector to its size. It means that we can release/free the extra unused allocated space for the vector and keep only the capacity up to its size, which is actually being used.

Free Resources