STL vector中的begin方法(3)

原文地址:http://www.cplusplus.com/reference/vector/vector/begin/

public member function
<vector>

std::vector::begin

      iterator begin();
const_iterator begin() const;
Return iterator to beginning

Returns an iterator pointing to the first element in the vector.

该方法返回一个指向该vector中第一个元素的iterator.


Notice that, unlike member vector::front, which returns a reference to the first element, this function returns a random access iterator pointing to it.

需要注意的是,和front()方法不同,front是返回第一个元素的引用,而begin返回的是一个指向第一个元素的随机访问迭代器。



If the container is empty, the returned iterator value shall not be dereferenced.

如果vector是空的,使用begin返回的迭代器不应该被解除引用。

例如:

#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
vector<int> vi;
vector<int>::iterator vb=vi.begin();
cout<<*vb<<endl;
}

编译运行结果:


可以看到,如果对其进行解除引用就会引发错误。


Parameters

none

参数:无

//以后这种超级简单的语句我就不翻译了


Return Value

返回值:

An iterator to the beginning of the sequence container.

指向该顺序容器第一个元素的迭代器。


If the vector object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.

如果该vector是const属性的,那么返回值也将是const属性的,否则,就是返回一个普通的iterator。


Member types iterator and const_iterator are random access iterator types (pointing to an element and to a const element, respectively).

返回值迭代器的类型属于随机访问迭代器


Example

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

int main ()
{
  std::vector<int> myvector;
  for (int i=1; i<=5; i++) myvector.push_back(i);

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}


Output:
myvector contains: 1 2 3 4 5

Complexity

Constant.

Iterator validity

No changes.

迭代器的有效性:

调用该方法后,不会影响其他迭代器的有效性。


Data races

数据的竞争性?:(这里可能不太准确)

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

//容器需要支持被访问?(不管返回值是哪个迭代器,都不会修改容器)

No contained elements are accessed by the call, but the iterator returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

//该调用不会访问容器的元素(??),但是返回的这个iterator可以用来访问或者是修改元素,同时通过该iteraotr访问或者是修改元素是安全的。

Exception safety

异常安全性:

No-throw guarantee: this member function never throws exceptions.

该方法不会抛出异常。

The copy construction or assignment of the returned iterator is also guaranteed to never throw.

//这句不知道怎么翻译才最好。

复制构造器或者(assignment of the returned iterator)一样不会抛出异常。

-----------------------------------------------------------------------------------------------------------

//已修正该句://2014-8-9

利用复制构造器或者是赋值运算符得到的该iterator也不会抛出异常

----------------------------------------------------------------------------------------------------

//翻译的不好的地方请指出来或者联系我修改,谢谢。


//2014-8-9 于GDUT


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值