std::vector::shrink_to_fit
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
void shrink_to_fit(); |
(C++11 起) | |
请删除未使用的容量.
原文:
Requests the removal of unused capacity.
这是一个不具约束力的要求,减少
capacity
size
。如果请求被满足,这取决于实施. 目录 |
[编辑] 参数
(无)
[编辑] 返回值
(无)
[编辑] 复杂性
常数
Example
#include <iostream> #include <vector> int main() { std::vector<int> v; std::cout << "Default-constructd capacity is " << v.capacity() << '\n'; v.resize(100); std::cout << "Capacity of a 100-element vector is " << v.capacity() << '\n'; v.clear(); std::cout << "Capacity after clear() is " << v.capacity() << '\n'; v.shrink_to_fit(); std::cout << "Capacity after shrink_to_fit() is " << v.capacity() << '\n'; }
输出:
Default-constructd capacity is 0 Capacity of a 100-element vector is 100 Capacity after clear() is 100 Capacity after shrink_to_fit() is 0
[编辑] 另请参阅
返回的元素数 (公共成员函数) | |
返回的元素的数目,可以保持在当前分配的存储空间 原文: returns the number of elements that can be held in currently allocated storage (公共成员函数) |