##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string::capacity

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

size_type capacity() const; |  |

Returns the number of characters that the string has currently allocated space for.

### [edit] Parameters

(none)

### [edit] Return value

capacity of the currently allocated storage

### [edit] Exceptions

(none) | (until C++11)
`noexcept` specification: noexcept | (since C++11)

### [edit] Complexity

Constant

### [edit] Example

Run this code

    #include <iostream>
    #include <string>

    void show_capacity(std::string const& s)
    {
        std::cout << "'" << s << "' has capacity " << s.capacity() << ".\n";
    }

    int main()
    {
        std::string s{"Exemplar"};
        show_capacity(s);

        s += " is an example string.";
        show_capacity(s);
    }

Possible output:

    'Exemplar' has capacity 8.
    'Exemplar is an example string.' has capacity 30.

### [edit] See also

size length | returns the number of characters (public member function) [edit]
reserve | reserves storage (public member function) [edit]

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

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template