How to Reduce the Capacity of a Vector in C++? Last Updated : 22 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the total amount of memory allocated to the vector is called the capacity of the vector. In this article, we will learn how we can reduce the capacity of a vector in C++.The most efficient method to reduce the capacity of vector is by using vector shrink_to_fit(). Let’s take a look at a simple example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v(11); cout << "Initial Capacity: " << v.capacity() << endl; // Reduce the size to 7 v.resize(7); // Shrink capacity of vector v.shrink_to_fit(); cout << "Final Capacity: " << v.capacity(); return 0; } OutputInitial Capacity: 11 Final Capacity: 7Explanation: Initially capacity of vector is 11, but after applying vector resize(), decrease the size of vector to 7 but not capacity. So to decrease the capacity of vector we use vector shrink_to_fit() which makes capacity equal to size of vector.There is also another method by which we can decrease the capacity of vector.Manually by Creating Another VectorTo reduce the capacity of a std::vector, we can first resize it to the desired size and then create a new vector with the same elements but no extra capacity by copying the elements into it and swap the vectors using vector swap(). C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v(7, 11); cout << "Initial Capacity: " << v.capacity() << endl; // Reduce size to 5 v.resize(5); // Create another vector of size 5 and copy // all elements vector<int> v1(v); // swap new vector with original vector v1.swap(v); cout << "Final Capacity: " << v.capacity(); return 0; } OutputInitial Capacity: 7 Final Capacity: 5Explanation: Initially the size and capacity of vector v is 7 but after using vector resize() make its size to 5 but capacity of vector v is still 7. Now we create another vector v1 with size and capacity 5 and copying all elements from original vector. On swapping both vector using vector swap() makes the capacity of vector v is 5. Comment More infoAdvertise with us Next Article How to Reduce the Capacity of a Vector in C++? mayankratre10 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find the Capacity of a Vector in C++? In C++, the capacity of a vector means the amount of space allocated for the vector, so the vector can hold more elements than its size without any reallocation of space. In this article, we will learn how to find the capacity of a vector in C++. Example: Input: myVector = {10,20,30}; Output: Capaci 2 min read How to Reduce the Capacity of a Vector to Fit Its Size in C++? In C++, vectors are dynamic arrays that can resize themselves when more data is needed. But internally, apart from the resizing, the vector also keeps some extra space for expected elements to reduce the time complexity of insertion. The total space accounting for this extra space is called capacity 2 min read How to Increase the Capacity of a Vector in C++? In C++, the capacity of vector indicates how many elements the vector can hold before it needs to reallocate additional memory. In this article, we will learn how to increase the capacity of vector in C++.The most effective way to increase the capacity of vector is by using vector reserve() method. 2 min read How to Find the Size of a Vector in C++? The size of a vector means the number of elements currently stored in the vector container. In this article, we will learn how to find the size of vector in C++.The easiest way to find the size of vector is by using vector size() function. Letâs take a look at a simple example:C++#include <bits/s 2 min read How to Find the Capacity of a Vector in Bytes in C++? In C++, vectors are used to store the collection of similar types of data and are able to automatically resize themself in order to accommodate more data. In this article, we will discuss how to calculate the capacity of a vector in bytes. For Example Input: myVector = {10, 34, 12, 90, 1}; Output: 2 2 min read How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in 2 min read How to Create a Vector of Pairs in C++? In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in 5 min read How to Find the Mode of Vector in C++? In C++, a vector is a dynamic array that resizes itself when needed. It also allows random access to its elements. In this article, we will learn how to find the mode of a vector in C++. The mode of a vector is the element that highest number of occurrences in the vector. Example: Input: myVector = 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read How to Find the Median of Sorted Vector in C++? In C++, vectors are dynamic arrays and the median is a middle value when data is sorted in ascending order. In this article, we will learn how to find the median of all elements in a sorted vector in C++. Example Input: myVector = {10, 20, 30, 40, 50}; Output: Median is 30Finding Median of Sorted Ve 2 min read Like