STL vector中的at方法(20)

原文地址:http://www.cplusplus.com/reference/vector/vector/at/
public member function
<vector>

std::vector::at

      reference at (size_type n);
const_reference at (size_type n) const;
Access element
Returns a reference to the element at position n in the vector.

返回一个指向位置n的元素的引用。


The function automatically checks whether n is within the bounds of valid elements in the vector, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds.

该方法将自动检测n是否是在一个有效的范围,如果不是则将抛出out_of_range异常。

另一个对照的方法是operator[],这个方法不会检测数据的有效性。

例子:

#include <iostream>
#include <vector>
#include <initializer_list>
using namespace std;
int main(int argc,char **argv)
{

	vector<int> vi({10,20,30});
	cout<<"vi.at(1)="<<vi.at(1)<<endl;
	cout<<"vi.at(10)="<<vi.at(10)<<endl;



}
运行结果:


可以看到,下标和数组一样,是从0开始的。

如果超出了范围,将会抛出异常。


Parameters

n
Position of an element in the container.
If this is greater than or equal to the vector size, an exception of type out_of_range is thrown.
Notice that the first element has a position of 0 (not 1).
Member type size_type is an unsigned integral type.

n是元素在vector中的位置。

如果n大于或者等于数组的size,那么将会抛出out_of_range异常。

注意,第一个元素的位置为0而不是1.

n的类型为unsigned intergral类型。

Return value

The element at the specified position in the container.

返回值为元素在容器内指定位置的值。


If the vector object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.

如果vector是只读属性的,那么返回的是一个const_reference.否则,是一个普通的引用。


Member types reference and const_reference are the reference types to the elements of the container (see vector member types).

引用类型为容器元素的引用。

Example

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

int main ()
{
  std::vector<int> myvector (10);   // 10 zero-initialized ints

  // assign some values:
  for (unsigned i=0; i<myvector.size(); i++)
    myvector.at(i)=i;

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); i++)
    std::cout << ' ' << myvector.at(i);
  std::cout << '\n';

  return 0;
}


Output:
myvector contains: 0 1 2 3 4 5 6 7 8 9

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed (neither the const nor the non-const versions modify the container).

容器将被访问。

The reference returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

返回的引用可以用来访问以及修改元素,同时这些操作都是安全的。


Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the container.

It throws out_of_range if n is out of bounds.

如果抛出异常,容器不会改变。当n不在范围内时抛出out_of_range异常。

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

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

2014-8-16

于GDUT



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值