std::hive<T,Allocator>::trim_capacity
From cppreference.com
void trim_capacity();
|
(1) | (since C++26) |
void trim_capacity( size_type new_cap );
|
(2) | (since C++26) |
1) deallocates all reserved blocks, and reduces
capacity() accordingly.All iterators (including the end() iterator), pointers and references to the elements of the container remain valid.
Parameters
| new_cap | - | requested new capacity of the hive, in number of elements |
Complexity
Linear in the number of deallocated reserved blocks.
Example
Run this code
#include <hive>
#include <print>
int main()
{
std::hive<int> hive;
const std::size_t count{1000};
const int value{100};
hive.insert(count, value);
hive.insert(count, value + 1);
std::println("After inserting {} elements capacity is {}", hive.size(), hive.capacity());
std::erase(hive, value);
std::println("After erasing {} elements capacity is {}", count, hive.capacity());
auto hive2 = hive;
hive.shrink_to_fit();
std::println("After shrink_to_fit() capacity is {}", hive.capacity());
hive2.trim_capacity();
std::println("After trim_capacity() capacity is {}", hive2.capacity());
}
Possible output:
After inserting 2000 elements capacity is 2000
After erasing 1000 elements capacity is 1000
After shrink_to_fit() capacity is 1000
After trim_capacity() capacity() is 1020
See also
| returns the number of elements that can be held in currently allocated storage (public member function) | |
| reduces memory usage by freeing unused memory (public member function) |