首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::vector::shrink_to_fit

void shrink_to_fit();

?

(since C++11)

请求清除未使用的容量。

这是一个不具有约束力的请求来减少capacity()size().这取决于请求是否得到满足的实现。

如果发生重新分配,则所有迭代器(包括结束迭代器的过去)和所有对元素的引用都无效。如果没有重新分配,则没有迭代器或引用无效。

参数

%280%29

类型要求

*。

-T必须符合MoveInserable的要求。

返回值

%280%29

复杂性

在容器的大小上最多是线性的。

注记

如果异常是由T%27移动构造函数引发的,则不会产生任何效果。

二次

代码语言:javascript
复制
#include <iostream>
#include <vector>
 
int main()
{
    std::vector<int> v;
    std::cout << "Default-constructed 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';
}

二次

可能的产出:

二次

代码语言:javascript
复制
Default-constructed capacity is 0
Capacity of a 100-element vector is 100
Capacity after clear() is 100
Capacity after shrink_to_fit() is 0

二次

另见

size

returns the number of elements (public member function)

capacity

returns the number of elements that can be held in currently allocated storage (public member function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com