std::basic_string::shrink_to_fit
| void shrink_to_fit(); |  | (since C++11) | 
|---|
请求清除未使用的容量。
这是一个不具有约束力的请求来减少capacity()到size().这取决于请求是否得到满足的实现。
如果发生重新分配,则所有指针、引用和迭代器都将失效。
参数
%280%29
返回值
%280%29
复杂性
常量。
例
二次
#include <iostream>
#include <string>
 
int main()
{
    std::string s;
    std::cout << "Default-constructed capacity is " << s.capacity() << '\n';
    s.resize(100);
    std::cout << "Capacity of a 100-element string is " << s.capacity() << '\n';
    s.clear();
    std::cout << "Capacity after clear() is " << s.capacity() << '\n';
    s.shrink_to_fit();
    std::cout << "Capacity after shrink_to_fit() is " << s.capacity() << '\n';
}二次
产出:
二次
Default-constructed capacity is 0
Capacity of a 100-element string is 100
Capacity after clear() is 100
Capacity after shrink_to_fit() is 0二次
另见
| sizelength | returns the number of characters (public member function) | 
|---|---|
| capacity | returns the number of characters that can be held in currently allocated storage (public member function) | 
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

