void shrink_to_fit(); // (1) C++11
constexpr void shrink_to_fit(); // (1) C++20
概要
領域をコンテナのサイズまで切り詰める
効果
capacity()
をsize()
に縮小させるリクエストを行う。- 実装依存の最適化を許可するために、縮小するという動作は仕様上強制されない。
- C++17 : この関数によって
capacity()
が増えることはない。 - C++17 :
capacity()
の縮小が起こる際に、メモリの再割り当てが発生する場合がある。その際、文字列の要素に対する参照、ポインタ、およびイテレータとそれが指す要素への参照は無効となる。
戻り値
なし
計算量
- C++17 : 最大で、要素数に対して線形時間
例
#include <iostream>
#include <string>
int main()
{
std::wstring s = L"The quick brown fox jumps over the lazy dog";
std::cout << s.capacity() << std::endl;
// 要素削除 : capacityは減らない
s.erase(s.begin() + 8, s.end());
std::cout << s.capacity() << std::endl;
// 領域を切り詰める
s.shrink_to_fit();
std::cout << s.capacity() << std::endl;
}
出力例
47
47
15
実装例
void basic_string::shrink_to_fit() {
swap(basic_string(*this));
}
バージョン
言語
- C++11
処理系
- Clang: 3.0, 3.1, 3.2, 3.4
- GCC: 4.5.4, 4.6.4, 4.7.3, 4.8.2
- ICC: ??
- Visual C++: 2010, 2012
関連項目
参照
- 『Effective STL - STLを効果的に使いこなす50の鉄則』 第17項 余分な容量を取り除くには「swap技法」を使おう
- [LWG Issue 755.
std::vector
andstd:string
lack explicit shrink-to-fit operations] - LWG Issue 2223.
shrink_to_fit
effect on iterator validity - P0980R1 Making
std::string
constexpr