Зміст курсу
C++ Data Types
C++ Data Types
Strings in Memory
As shown in the video, strings have length and capacity.
They can be accessed using .length()
and .capacity()
methods.
main
There are also two methods to work with capacity, .reserve(n)
and shrink_to_fit()
.
.reserve(n)
– set capacity to at leastn
;.shrink_to_fit()
– set capacity the same as the length.
So the .shrink_to_fit()
is useful when you know you will not extend your string, but the capacity is greater than the length. And you can use .reserve(n)
if your string is likely to be appended multiple times.
main
Все було зрозуміло?