C++ 字符串库 - shrink_to_fit



描述

它请求字符串减小其容量以适应其大小。

声明

以下是 std::string::shrink_to_fit 的声明。

void shrink_to_fit();

C++11

void shrink_to_fit();

参数

返回值

异常

如果抛出异常,则字符串不会发生任何更改。

示例

以下示例演示 std::string::shrink_to_fit。

#include <iostream>
#include <string>

int main () {
   std::string str (500,'x');
   std::cout << "1. capacity of str: " << str.capacity() << '\n';

   str.resize(10);
   std::cout << "2. capacity of str: " << str.capacity() << '\n';

   str.shrink_to_fit();
   std::cout << "3. capacity of str: " << str.capacity() << '\n';

   return 0;
}

示例输出应如下所示:

1. capacity of str: 500
2. capacity of str: 500
3. capacity of str: 10
string.htm
广告
© . All rights reserved.