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
廣告