public member function
<string>
std::basic_string::capacity
size_type capacity() const;
size_type capacity() const noexcept;
Return size of allocated storage
This capacity is not necessarily equal to the string length. It can be equal or greater, with the extra space allowing the object to optimize its operations when new characters are added to the basic_string.
Notice that this capacity does not suppose a limit on the length of the basic_string. When this capacity is exhausted and more is needed, it is automatically expanded by the object (reallocating it storage space). The theoretical limit on the length of a basic_string is given by member max_size.
The capacity of a basic_string can be altered any time the object is modified, even if this modification implies a reduction in size or if the capacity has not been exhausted (this is in contrast with the guarantees given to capacity in vector containers).
The capacity of a basic_string can be explicitly altered by calling member reserve.
Parameters
noneReturn Value
The size of the storage capacity currently allocated for the basic_string.Member type
size_type is an unsigned integral type.Example
|
|
A possible output for this program could be:
size: 11 length: 11 capacity: 15 max_size: 429496729
Complexity
Unspecified, but generally constant.Iterator validity
No changes.Data races
The object is accessed.Exception safety
No-throw guarantee: this member function never throws exceptions.See also
- basic_string::reserve
- Request a change in capacity (public member function)
- basic_string::length
- Return length of string (public member function)
- basic_string::size
- Return size (public member function)
- basic_string::max_size
- Return maximum size (public member function)