std::vector::shrink_to_fit(3)
π std::vector::shrink_to_fit(3) β the C++ Standard Library, bestie
okay so we're in the C++ Standard Library today (std::vector::shrink_to_fit), and this one's a little icon of "please free my memory... if you feel like it." β¨
πΈ NAME
std::vector::shrink_to_fit β literally the same thing as std::vector::shrink_to_fit, no notes π
β¨ Synopsis
void shrink_to_fit(); (constexpr since C++20)
Requests the removal of unused capacity. π
It's a non-binding request to reduce capacity() to size(). Whether the request actually gets fulfilled depends on the implementation β so like, you asked nicely, but nobody promised you anything, yk? π
If reallocation occurs, all iterators (including the end() iterator) and all references to the elements are invalidated. If no reallocation occurs, no iterators or references are invalidated. π
π¦ Parameters
(none)
π Type requirements
Tmust meet the requirements ofMoveInsertableinto*this. (since C++11)
π Return value
(none)
β Complexity
At most linear in the size of the container.
π₯ Exceptions
(since C++11)
If an exception is thrown other than by the move constructor of a non-CopyInsertable T, there are no effects. π«Ά
π Notes
In libstdc++, shrink_to_fit() is not available in C++98 mode. periodt.
π Example
// Run this code
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v;
std::cout << "Default-constructed capacity is " << v.capacity() <<
'\n';
v.resize(100);
std::cout << "Capacity of a 100-element vector is " << v.capacity()
<< '\n';
v.resize(50);
std::cout << "Capacity after resize(50) is " << v.capacity() <<
'\n';
v.shrink_to_fit();
std::cout << "Capacity after shrink_to_fit() is " << v.capacity() <<
'\n';
v.clear();
std::cout << "Capacity after clear() is " << v.capacity() << '\n';
v.shrink_to_fit();
std::cout << "Capacity after shrink_to_fit() is " << v.capacity() <<
'\n';
for (int i = 1000; i < 1300; ++i)
v.push_back(i);
std::cout << "Capacity after adding 300 elements is " <<
v.capacity() << '\n';
v.shrink_to_fit();
std::cout << "Capacity after shrink_to_fit() is " << v.capacity() <<
'\n';
}
π Possible output
Default-constructed capacity is 0
Capacity of a 100-element vector is 100
Capacity after resize(50) is 100
Capacity after shrink_to_fit() is 50
Capacity after clear() is 50
Capacity after shrink_to_fit() is 0
Capacity after adding 300 elements is 512
Capacity after shrink_to_fit() is 300
π Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (yes, they went back and edited history, main character energy fr fr)
- LWG 755, applied to C++98 β behavior as published:
std::vectorlacked explicit shrink-to-fit operations. Correct behavior: provided. π - LWG 2033, applied to C++11 β behavior as published: 1.
Twas not required to beMoveInsertable2. the complexity requirement was missing. Correct behavior: 1. required 2. added. π₯
πΈ See also
sizeβ returns the number of elements (public member function)capacityβ returns the number of elements that can be held in currently allocated storage (public member function)
okay that's the whole page, bestie, you ate that π«Ά go shrink some capacity (or don't, it's non-binding, live your truth) π
source/version: http://cppreference.com β dated 2024.06.10 β std::vector::shrink_to_fit(3) signing off π§π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms