STL中的size()方法(13)

故名思议,size()就是vector的大小,即元素的个数。
public member function
<vector>

std::vector::size

size_type size() const noexcept;
Return size
Returns the number of elements in the vector.

返回vector中元素的个数。


This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity.

这个个数是vector中实际存放的元素个数,但不一定必须等于这个vector的容量


Parameters

none

Return Value

The number of elements in the container.
返回值为元素个数。

Member type size_type is an unsigned integral type.

类型为无符号整数。


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// vector::size
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myints;
  std::cout << "0. size: " << myints.size() << '\n';

  for (int i=0; i<10; i++) myints.push_back(i);
  std::cout << "1. size: " << myints.size() << '\n';

  myints.insert (myints.end(),10,100);
  std::cout << "2. size: " << myints.size() << '\n';

  myints.pop_back();
  std::cout << "3. size: " << myints.size() << '\n';

  return 0;
}


Output:
0. size: 0
1. size: 10
2. size: 20
3. size: 19

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值