名前空間
変種
操作

std::basic_string<CharT,Traits,Allocator>::shrink_to_fit

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
void shrink_to_fit();
(C++11以上)
(C++20未満)
constexpr void shrink_to_fit();
(C++20以上)

未使用の容量の削除を要求します。

これは capacity()size() に減らす拘束力のない要求です。 要求が満たされるかどうかは処理系に依存します。

再確保が行われた場合、すべてのポインタ、参照、イテレータは無効化されます。

目次

[編集] 引数

(なし)

[編集] 戻り値

(なし)

[編集] 計算量

(未規定)

(C++17未満)

文字列のサイズに比例

(C++17以上)

[編集]

#include <iostream>
#include <string>
 
int main()
{
    std::string s;
    std::cout << "Default-constructed capacity is " << s.capacity() 
        << " and size is " << s.size() << '\n';
    for (int i=0; i<42; i++)
        s.append(" 42 ");
    std::cout << "Capacity after a couple of appends is " << s.capacity() 
        << " and size is " << s.size() << '\n';
    s.clear();
    std::cout << "Capacity after clear() is " << s.capacity() 
        << " and size is " << s.size() << '\n';
    s.shrink_to_fit();
    std::cout << "Capacity after shrink_to_fit() is " << s.capacity() 
        << " and size is " << s.size() << '\n';
}

出力例:

Default-constructed capacity is 15 and size 0
Capacity after a couple of appends is 240 and size 168
Capacity after clear() is 240 and size 0
Capacity after shrink_to_fit() is 15 and size 0

[編集] 関連項目

文字数を返します
(パブリックメンバ関数) [edit]
現在確保されている記憶域に保持することができる文字数を返します
(パブリックメンバ関数) [edit]