C++ String shrink_to_fit()

2024年8月30日 | 阅读 1 分钟

此函数会减少字符串的容量 (capacity),使其等于其大小 (size)。

语法

假设有一个字符串 str,语法将是:

参数

它不包含任何参数。

返回值

它不返回任何值。

示例 1

让我们来看一个简单的例子。

输出

15
15

在此示例中,shrink_to_fit() 函数被应用于字符串,以便字符串的容量变得等于字符串的大小。

示例 2

我们来看另一个简单的示例。

输出

Initial string value is: Computer is my favorite subject
After resizing, string value is: Computer is my favorite 
capacity of the string is :24
size of the string is  :24

下一主题C++ 字符串