const_iterator cbegin() const noexcept; // (1) C++11
constexpr const_iterator cbegin() const noexcept; // (1) C++20
概要
先頭の要素を指す読み取り専用イテレータを取得する。
begin()
は非const
なvector
オブジェクトに対してiterator
を返し、const
なvector
オブジェクトに対してはconst_iterator
を返すが、cbegin()
はconst_iterator
を返すバージョンのみが提供されている。
アルゴリズムにイテレータの組を渡す際、アルゴリズム内でデータの書き換えが起こらないというユーザーの意図を示す場合などに有用である。
戻り値
先頭の要素を指す読み取り専用イテレータ
例外
投げない
計算量
定数時間
例
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> v = {1, 2, 3};
// このアルゴリズム内ではvの書き換えを決して行わない
std::for_each(v.cbegin(), v.cend(), [](const int& x) {
std::cout << x << std::endl;
});
}
出力
1
2
3
バージョン
言語
- C++11
処理系
- Clang: ??
- GCC: 4.7.0
- ICC: ??
- Visual C++: 2010, 2012, 2013