Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

vector
c++
communitycreator
shrink_to_fit

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

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 $1$ and then insert that element into it. If we try to insert one element again, it will double its capacity to $2$ and then insert the new element. Similarly, if we want to insert one more element, it will double its capacity to $4$ 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 $4$, 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;
}

Using vector::shrink_to_fit() function in C++

### 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 $8$, and that can be understood by the explanation given above at the starting.

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

• 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 $8$ 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 $8$ to $3$.

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.

RELATED TAGS

vector
c++
communitycreator
shrink_to_fit

CONTRIBUTOR Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Learn in-demand tech skills in half the time 