20 Strings library [strings]

20.3 String classes [string.classes]

20.3.2 Class template basic_­string [basic.string]

20.3.2.4 Capacity [string.capacity]

size_type size() const noexcept; size_type length() const noexcept;
Returns: A count of the number of char-like objects currently in the string.
Complexity: Constant time.
size_type max_size() const noexcept;
Returns: The largest possible number of char-like objects that can be stored in a basic_­string.
Complexity: Constant time.
void resize(size_type n, charT c);
Effects: Alters the value of *this as follows:
  • If n <= size(), erases the last size() - n elements.
  • If n > size(), appends n - size() copies of c.
void resize(size_type n);
Effects: Equivalent to resize(n, charT()).
size_type capacity() const noexcept;
Returns: The size of the allocated storage in the string.
Complexity: Constant time.
void reserve(size_type res_arg);
Effects: A directive that informs a basic_­string of a planned change in size, so that the storage allocation can be managed accordingly.
After reserve(), capacity() is greater or equal to the argument of reserve if reallocation happens; and equal to the previous value of capacity() otherwise.
Reallocation happens at this point if and only if the current capacity is less than the argument of reserve().
Throws: length_­error if res_­arg > max_­size() or any exceptions thrown by allocator_­traits <Allocator>::allocate.
void shrink_to_fit();
Effects: shrink_­to_­fit is a non-binding request to reduce capacity() to size().
[Note
:
The request is non-binding to allow latitude for implementation-specific optimizations.
end note
]
It does not increase capacity(), but may reduce capacity() by causing reallocation.
Complexity: Linear in the size of the sequence.
Remarks: Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence as well as the past-the-end iterator.
If no reallocation happens, they remain valid.
void clear() noexcept;
Effects: Equivalent to: erase(begin(), end());
[[nodiscard]] bool empty() const noexcept;
Effects: Equivalent to: return size() == 0;