STL vector中的capacity()方法(14)

capacity和size是有所区别的!
public member function
<vector>

std::vector::capacity

size_type capacity() const noexcept;
Return size of allocated storage capacity
Returns the size of the storage space currently allocated for the vector, expressed in terms of elements. 返回当前为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 membermax_size.
要注意的是capacity并不是vector所支持的最大容量,当容量被耗尽或者是需要比容量更多的空间时,vector会自动通过重新分配存储空间来完成增长。理论上最大的容量可以从成员方法max_size中获得。

The capacity of a vector can be explicitly altered by calling member vector::reserve.
容量可以通过调用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.
返回值为当前vector所分配的内存空间。用于衡量当前vector所能存放的元素数目。

Member type size_type is an unsigned integral type.
返回值类型s为无符号整型。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// comparing size, capacity and max_size
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;

  // set some content in the vector:
  for (int i=0; i<100; i++) myvector.push_back(i);

  std::cout << "size: " << (int) myvector.size() << '\n';
  std::cout << "capacity: " << (int) myvector.capacity() << '\n';
  std::cout << "max_size: " << (int) myvector.max_size() << '\n';
  return 0;
}


A possible output for this program could be:
size: 100
capacity: 128
max_size: 1073741823

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed.
No contained elements are accessed: concurrently accessing or modifying them is safe.
容器将被访问。 容器内的元素不会被访问。

Exception safety

No-throw guarantee: this member function never throws exceptions.
该成员方法不会抛出异常。

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。


转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-12

于GDUT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值