##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string::shrink_to_fit

From cppreference.com
< cpp‎ | string‎ | basic string

void shrink_to_fit(); |  | (since C++11)

Requests the removal of unused capacity.

It is a non-binding request to reduce capacity to size. It depends on the implementation if the request is fulfilled.

### [edit] Parameters

(none)

### [edit] Return value

(none)

### [edit] Complexity

Constant

### [edit] Example

Run this code

    #include <iostream>
    #include <string>

    int main()
    {
        std::string s;
        std::cout << "Default-constructed capacity is " << s.capacity() << '\n';
        s.resize(100);
        std::cout << "Capacity of a 100-element string is " << s.capacity() << '\n';
        s.clear();
        std::cout << "Capacity after clear() is " << s.capacity() << '\n';
        s.shrink_to_fit();
        std::cout << "Capacity after shrink_to_fit() is " << s.capacity() << '\n';
    }

Output:

    Default-constructed capacity is 0
    Capacity of a 100-element string is 100
    Capacity after clear() is 100
    Capacity after shrink_to_fit() is 0

### [edit] See also

size length | returns the number of characters (public member function) [edit]
capacity | returns the number of characters that can be held in currently allocated storage (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/shrink_to_fit&oldid=63427"