std::basic_string::shrink_to_fit
来自cppreference.com
< cpp | string | basic string
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
void shrink_to_fit(); |
(C++11 起) | |
请删除未使用的容量.
原文:
Requests the removal of unused capacity.
这是一个不具约束力的要求,减少
capacity
size
。如果请求被满足,这取决于实施. 目录 |
[编辑] 参数
(无)
[编辑] 返回值
(无)
[编辑] 复杂度
常数
[编辑] 示例
#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
[编辑] 另请参阅
returns the number of characters (公共成员函数) | |
返回数字的字符,可以保持在当前分配的存储空间 原文: returns the number of characters that can be held in currently allocated storage (公共成员函数) |