Size versus Capacity
The number of elements a string has (str.size()
) is in general smaller than the number of elements, for which space is reserved: str.capacity()
. Therefore if you add elements to a string, there will not automatically be new memory allocated. std:max_size()
return how many elements a string can maximal have. For the three methods the following relation holds: str.size() <= str.capacity() <= str.max_size()
.
The following table shows the methods for dealing with the memory management of the string.
Methods | Description |
---|---|
str.empty() |
Checks if str has elements. |
str.size(), str.length() |
Number of elements of the str . |
str.capacity() |
Number of elements str can have without reallocation. |
str.max_size() |
Number of elements str can maximal have. |
str.resize(n) |
Increases str... |