public member function
<string>

std::basic_string::shrink_to_fit

void shrink_to_fit();
收縮到合適大小
請求 basic_string 減小其 容量 以適應其 大小

該請求不具強制性,容器實現可以自由地進行其他最佳化,並使 basic_string容量 大於其 大小

此函式對字串長度沒有影響,也不能改變其內容。

引數



返回值



示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// string::shrink_to_fit
#include <iostream>
#include <string>

int main ()
{
  std::string str (100,'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: 100
2. capacity of str: 100
3. capacity of str: 10


複雜度

未指定,但通常是恆定的。

迭代器有效性

與此物件相關的任何迭代器、指標和引用都可能失效。

資料競爭

物件被修改。

異常安全

強保證: 如果丟擲異常,則 basic_string 中沒有任何更改。

如果該型別使用預設分配器,如果函式需要分配儲存空間但失敗,則會丟擲bad_alloc異常。

另見