vector::capacity

网友投稿 257 2022-11-29

vector::capacity

vector::capacity(容量)

Return size of allocated storage capacity

Returns the size of the storage space currently allocated for the ​​vector​​, expressed in terms of elements.

This capacity is not necessarily equal to the ​​vector size​​. It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion.

Notice that this capacity does not suppose a limit on the size of the ​​vector​​. When this capacity is exhausted and more is needed, it is automatically expanded by the container (reallocating it storage space). The theoretical limit on the ​​size​​​ of a ​​vector​​​ is given by member ​​max_size​​.

The capacity of a ​​vector​​​ can be explicitly altered by calling member ​​vector::reserve​​.

Parameters

none

Return Value

The size of the currently allocated storage capacity in the ​​vector​​, measured in terms of the number elements it can hold.

Member type size_type

capacity的意思是容量,此方法返回的是该vector对象最多能容纳多少个元素。 size的意思是大小,此方法是返回该vector对象当前有多少个元素。

// comparing size, capacity and max_size#include #include using namespace std;int main (){ std::vector myvector; // set some content in the vector: for (int i=0; i<100; i++) myvector.push_back(i); cout << "size: " << (int) myvector.size() << '\n'; cout << "capacity: " << (int) myvector.capacity() << '\n'; cout << "max_size: " << (int) myvector.max_size() << '\n'; return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:@RequestParam 参数偶尔丢失的解决
下一篇:HDU 5793 A Boring Question (找规律+快速幂)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~