shrink_to_fit
描述 (Description)
它请求字符串减少其容量以适应其大小。
声明 (Declaration)
以下是std :: string :: shrink_to_fit的声明。
void shrink_to_fit();
C++11
void shrink_to_fit();
参数 (Parameters)
没有
返回值 (Return Value)
没有
异常 (Exceptions)
如果抛出异常,则字符串中没有变化。
例子 (Example)
在下面的示例中,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