Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/shrink to fit"

From cppreference.com
m (Fixed typo: "constructd" in example code & results as it affected cpp/container/vector/shrink_to_fit)
m (Synopsis: +(constexpr since C++20) to *)
 
(20 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | shrink_to_fit}}
+
{{cpp/container/{{{1|}}}/title|shrink_to_fit}}
 
{{cpp/container/{{{1|}}}/navbar}}
 
{{cpp/container/{{{1|}}}/navbar}}
{{ddcl | notes={{mark since c++11}} |
+
{{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 reduce {{#ifeq:{{{1|}}}|vector|{{rlpt|capacity}}|{{tt|capacity}}}} to {{rlpt|size}}. It depends on the implementation if the request is fulfilled.  
+
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===
 
===Parameters===
 
(none)
 
(none)
 +
 +
{{par begin}}
 +
{{par hreq}}
 +
{{par req insertable|T|MoveInsertable|notes={{mark since c++11}}}}
 +
{{par end}}
  
 
===Return value===
 
===Return value===
Line 15: Line 31:
  
 
===Complexity===
 
===Complexity===
Constant
+
At most linear in the size of the container.
  
{{#ifeq: {{{1|}}}|vector|===Example===
+
{{rrev|since=c++11|
{{example
+
===Exceptions===
|
+
If an exception is thrown other than by the move constructor of a non-{{named req|CopyInsertable}} {{tt|T}}, there are no effects.
| code=
+
}}
#include <iostream>
+
#include <{{{1}}}>
+
int main()
+
{
+
    std::{{{1}}}<int> v;
+
    std::cout << "Default-constructed capacity is " << v.capacity() << '\n';
+
    v.resize(100);
+
    std::cout << "Capacity of a 100-element {{{1}}} 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';
+
}
+
| output=
+
Default-constructed capacity is 0
+
Capacity of a 100-element {{{1}}} is 100
+
Capacity after clear() is 100
+
Capacity after shrink_to_fit() is 0
+
}}}}
+
===See also===
+
  
{{dcl list begin}}
+
===Notes===
{{dcl list template | cpp/container/dcl list size |{{{1|}}}}}
+
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.
{{#ifeq: {{{1|}}}|vector|{{dcl list template | cpp/container/dcl list capacity |{{{1|}}}}}}}
+
 
{{dcl list end}}
+
===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.

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.

Exceptions

If an exception is thrown other than by the move constructor of a non-CopyInsertable T, there are no effects.

(since C++11)

[edit] Notes

In libstdc++, shrink_to_fit() is not available in C++98 mode.

[edit] Example

[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 MoveInsertable
2. the complexity requirement was missing
1. required
2. added

[edit] See also

returns the number of elements
(public member function of std::{{{1}}}) [edit]