std::vector::shrink_to_fit(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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

  • T must meet the requirements of MoveInsertable into *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::vector lacked explicit shrink-to-fit operations. Correct behavior: provided. πŸ’€
  • LWG 2033, applied to C++11 β€” behavior as published: 1. T was not required to be MoveInsertable 2. 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