Difference between revisions of "Template:cpp/container/shrink to fit"
From cppreference.com
(37 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/container/{{{1|}}}/title | shrink_to_fit}} | + | {{cpp/container/{{{1|}}}/title|shrink_to_fit}} |
− | {{cpp/container/{{{1|}}}/ | + | {{cpp/container/{{{1|}}}/navbar}} |
− | {{ | + | {{dcl begin}} |
+ | {{#ifeq:{{{1}}}|vector| | ||
+ | {{dcl|notes={{mark constexpr since c++20}}| | ||
void shrink_to_fit(); | void shrink_to_fit(); | ||
}} | }} | ||
+ | | | ||
+ | {{dcl| | ||
+ | void shrink_to_fit(); | ||
+ | }} | ||
+ | }} | ||
+ | {{dcl end}} | ||
+ | |||
Requests the removal of unused capacity. | Requests the removal of unused capacity. | ||
− | It is a non-binding request to | + | It is a non-binding request to {{#ifeq:{{{1|}}}|vector|reduce {{lc|capacity()}} to {{lc|size()}}|reduce the memory usage without changing the size of the sequence}}. It depends on the implementation whether the request is fulfilled. |
− | + | {{cpp/container/note iterator invalidation|{{{1|}}}|shrink_to_fit}} | |
− | {{ | + | |
− | {{ | + | ===Parameters=== |
+ | (none) | ||
+ | |||
+ | {{par begin}} | ||
+ | {{par hreq}} | ||
+ | {{par req insertable|T|MoveInsertable|notes={{mark since c++11}}}} | ||
+ | {{par end}} | ||
+ | |||
+ | ===Return value=== | ||
+ | (none) | ||
+ | |||
+ | ===Complexity=== | ||
+ | At most linear in the size of the container. | ||
+ | |||
+ | {{rrev|since=c++11| | ||
+ | ===Exceptions=== | ||
+ | If an exception is thrown other than by the move constructor of a non-{{named req|CopyInsertable}} {{tt|T}}, there are no effects. | ||
+ | }} | ||
− | + | ===Notes=== | |
− | + | In libstdc++, {{tt|shrink_to_fit()}} is [https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html#strings.string.shrink not available] in C++98 mode. | |
− | {{ | + | |
− | + | ===Example=== | |
− | {{ | + | {{include|cpp/container/{{{1|}}}/example_shrink_to_fit}} |
− | + | ||
− | + | ===Defect reports=== | |
− | {{ | + | {{dr list begin}} |
+ | {{dr list item|wg=lwg|dr={{#ifeq:{{{1|}}}|vector|755|850}}|std=C++98|before={{tt|std::{{{1|}}}}} lacked explicit shrink-to-fit operations|after=provided}} | ||
+ | {{dr list item|wg=lwg|dr=2033|std=C++11|before=1. {{tt|T}} was not required to be {{named req|MoveInsertable}}<br>2. the complexity requirement was missing|after=1. required<br>2. added}} | ||
+ | {{dr list end}} | ||
− | {{ | + | ===See also=== |
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc|cpp/container/dsc size|{{{1|}}}}} |
+ | {{#ifeq:{{{1|}}}|vector|{{dsc inc|cpp/container/dsc capacity|{{{1|}}}}}}} | ||
+ | {{dsc end}} |
Latest revision as of 15:15, 5 May 2024
void shrink_to_fit(); |
||
Requests the removal of unused capacity.
It is a non-binding request to reduce the memory usage without changing the size of the sequence. It depends on the implementation whether the request is fulfilled.
Information on iterator invalidation is copied from here |
Contents |
[edit] Parameters
(none)
Type requirements | ||
-T must meet the requirements of MoveInsertable into *this. (since C++11)
|
[edit] Return value
(none)
[edit] Complexity
At most linear in the size of the container.
ExceptionsIf an exception is thrown other than by the move constructor of a non-CopyInsertable |
(since C++11) |
[edit] Notes
In libstdc++, shrink_to_fit()
is not available in C++98 mode.
[edit] Example
Templated content. The link is cpp/container/<PP>/example_shrink_to_fit |
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 850 | C++98 | std:: lacked explicit shrink-to-fit operations
|
provided |
LWG 2033 | C++11 | 1. T was not required to be MoveInsertable2. the complexity requirement was missing |
1. required 2. added |
[edit] See also
returns the number of elements (public member function of std::{{{1}}} )
|