STLvector中的begin方法(3)
原文地址:http://www.cplusplus.com/reference/vector/vector/begin/
public member function
std::vector::begin
- C++98
- C++11
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
#include
#include
using namespace std;
int main()
{
vector
vector
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 | 1 |
1 | // vector::begin/end |
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
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/stlvector中的begin方法3/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!