
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ vector::shrink_to_fit() Function
Before going to learn about the shrink_to_fit() function. Lets have a look on these two terms −
- Capacity − The capacity() function is a built-in function that returns the vector's current storage space allocation, expressed in terms of elements. This capacity is not necessarily equal to the vector size.
- Size − The size() function is used to return the size of vector container or the number of elements in the container. The amount of memory that a vector is currently utilizing is indicated by capacity().
The shrink_to_fit() function instructs the vector container to shrink its capacity to its size. In other words, the capacity will become equal to the actual number of elements that are contained inside the vector container.
Syntax
Following is the syntax for C++ vector::shrink_to_fit() Function −
void shrink_to_fit();
Parameters
It doesn't accept any kind of parameter.
Example 1
Let's consider the following example, where we are going to use shrink_to_fit() function.
#include <iostream> #include <vector> using namespace std; int main(void) { vector<int> v(128); cout << "Initial capacity = " << v.capacity() << endl; v.resize(25); cout << "Capacity after resize = " << v.capacity() << endl; v.shrink_to_fit(); cout << "Capacity after shrink_to_fit = " << v.capacity() << endl; return 0; }
Output
When we compile and run the above program, this will produce the following result −
Initial capacity = 128 Capacity after resize = 128 Capacity after shrink_to_fit = 25
Example 2
Considering the another scenario, where we are going to take an string type and applying shrink_to_fit() function.
#include <iostream> #include <vector> using namespace std; int main(void) { vector<string> myvector(50); cout << "Initial capacity = " << myvector.capacity() << endl; myvector.resize(13); cout << "Capacity after resize = " << myvector.capacity() << endl; myvector.shrink_to_fit(); cout << "Capacity after shrink_to_fit = " << myvector.capacity() << endl; return 0; }
Output
On running the above program, it will produce the following result −
Initial capacity = 50 Capacity after resize = 50 Capacity after shrink_to_fit = 13
Example 3
In the following example, we are going to use push_back() function to insert the value and then applying the shrink_to_fit() function.
#include <iostream> #include <vector> using namespace std; void print_vector(vector<int> myvector){ for(int x: myvector) cout << x << " "; } int main(){ vector<int> myvector; myvector.push_back(11); myvector.push_back(22); myvector.push_back(33); cout << "Actual vector: "; print_vector(myvector); cout << endl; cout << "Capacity of vector is: " << myvector.capacity()<<endl; myvector.resize(2); cout << "After Resize(2): "; print_vector(myvector); cout << endl; cout << "Capacity of vector is: " << myvector.capacity()<<endl; myvector.shrink_to_fit(); cout << "after using shrink_to_fit() fuction vector capacity is: "<<myvector.capacity(); return 0; }
Output
When we execute the above program, it will produce the following result −
Actual vector: 11 22 33 Capacity of vector is: 4 After Resize(2): 11 22 Capacity of vector is: 4 after using shrink_to_fit() fuction vector capacity is: 2