STL vector中的operator[]方法(31)

public member function
<vector>

std::vector::operator[]

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

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


A similar member function, vector::at, has the same behavior as this operator function, except that vector::at is bound-checked and signals if the requested position is out of range by throwing an out_of_range exception. 

一个相似的方法是at,和该方法具有一样的行为,不同的是at是检测边界的,并且当请求的位置超出范围后会抛出out_of_range异常。


Portable programs should never call this function with an argument n that is out of range, since this causes undefined behavior.

不应该在n超出范围的时候调用该方法,这会导致未知的错误。

例子:

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	vector<int> vi({1,2,3,4,5});
	cout<<"vi[0]="<<vi[0]<<endl;
	cout<<"vi[10]="<<vi[10]<<endl;




}
结果截图:



这里的vi[10]虽然未定义,但是还是会有默认的值。但这是不对的。


Parameters

n
Position of an element in the container.
Notice that the first element has a position of 0 (not 1).
Member type size_type is an unsigned integral type.

n是元素在容器中的位置。

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

n是一个无符号整数。

Return value

The element at the specified position in the vector.

返回值为在指定位置的元素的引用。


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

如果vector对象是const属性的,返回的引用也是const的,否则,返回一个普通的引用。


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
20
21
22
23
24
25
26
27
28
29
// vector::operator[]
#include <iostream>
#include <vector>

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

  std::vector<int>::size_type sz = myvector.size();

  // assign some values:
  for (unsigned i=0; i<sz; i++) myvector[i]=i;

  // reverse vector using operator[]:
  for (unsigned i=0; i<sz/2; i++)
  {
    int temp;
    temp = myvector[sz-1-i];
    myvector[sz-1-i]=myvector[i];
    myvector[i]=temp;
  }

  std::cout << "myvector contains:";
  for (unsigned i=0; i<sz; i++)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}


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

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

If the container size is greater than n, the function never throws exceptions (no-throw guarantee).

如果容器的大小大于n,不会抛出异常。

Otherwise, the behavior is undefined.

否则,将导致未知的错误。


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

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

2014-8-19

于GDUT



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值