天天看點

vector::cbegin (c++11)

//其實這個和begin差不多,隻是具有了const屬性,不能用于修改元素而已。

public member function

<vector>

std::vector::cbegin

const_iterator cbegin() const noexcept;      

Return const_iterator to beginning

Returns a const_iterator pointing to the first element in the container.

傳回一個const_iterator指向容器的第一個元素。

A const_iterator

and decreased (unless it is itself also const), just like the iterator returned by ​​vector::begin​​​, but it cannot be used to modify the contents it points to, even if the ​​vector​​ object is not itself const.

一個const_iterator是一種類似于指向常量的疊代器,他們可以被遞增或是遞減(除非這個iterator本身是常量才不能這樣),這就像和begin()傳回的iterator一樣,隻是cbegin()傳回的疊代器不能用于修改該元素而已,甚至vector本身并不是const屬性,該方法傳回的iterator也是不能用于修改元素的.

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

如果容器為空,不應該對該iterator解除引用(即對該iterator取*)

Parameters

none

Return Value

A const_iterator to the beginning of the sequence.

一個指向序列第一個元素的具有const屬性的iterator.

Member type const_iterator is a ​​random access iterator​​ type that points to a const element.

該const_iterator屬于随機通路疊代器,指向一個具有const屬性的元素(該元素本身可能并不是const的)

// vector::cbegin/cend
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
  vector<int> myvector = {10,20,30,40,50};
  cout << "myvector contains:";
  for(auto it = myvector.cbegin(); it != myvector.cend(); ++it)
    cout << ' ' << *it;
    cout << '\n';
  return 0;
}      

Output:

myvector contains: 10 20 30 40 50

Complexity

Constant.

Iterator validity

No changes.

該方法不會對其他疊代器的有效性造成影響。

Data races

The container is accessed.

該容器應該是可通路的。

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

該方法不會通路容器裡的元素,但是傳回的這個iterator可以用來通路元素,并且用他們來通路或者是修改不同的元素都是安全的。(這裡似乎有點問題?運用const_cast轉換?這裡似乎轉化不了,什麼情況?)

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

該方法不會抛出異常。