[string.capacity] (original) (raw)
21 Strings library [strings]
21.3 String classes [string.classes]
21.3.2 Class template basic_string [basic.string]
21.3.2.4 Capacity [string.capacity]
constexpr size_type size() const noexcept;constexpr size_type length() const noexcept;
Returns:A count of the number of char-like objects currently in the string.
Complexity:Constant time.
constexpr size_type max_size() const noexcept;
Returns:The largest possible number of char-like objects that can be stored in abasic_string.
Complexity:Constant time.
constexpr void resize(size_type n, charT c);
Effects:Alters the value of*thisas follows:
- Ifn <= size(), erases the last size() - n elements.
- Ifn > size(), appends n - size() copies of c.
constexpr void resize(size_type n);
Effects:Equivalent to resize(n, charT()).
constexpr size_type capacity() const noexcept;
Returns:The size of the allocated storage in the string.
Complexity:Constant time.
constexpr 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.
Afterreserve(),capacity()is greater or equal to the argument ofreserveif reallocation happens; and equal to the previous value ofcapacity()otherwise.
Reallocation happens at this point if and only if the current capacity is less than the argument of reserve().
Throws: length_errorifres_arg > max_size() or any exceptions thrown byallocator_traits <Allocator>::allocate.
constexpr void shrink_to_fit();
Effects: shrink_to_fit is a non-binding request to reducecapacity() 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:If the size is not equal to the old capacity, linear in the size of the sequence; otherwise constant.
Remarks:Reallocation invalidates all the references, pointers, and iterators referring to the elements in the sequence, as well as the past-the-end iterator.
[ Note
:
If no reallocation happens, they remain valid.
— end note
]
constexpr void clear() noexcept;
Effects:Equivalent to: erase(begin(), end());
[[nodiscard]] constexpr bool empty() const noexcept;
Effects:Equivalent to:return size() == 0;