const_iterator cend() const noexcept; // (1) C++11
constexpr const_iterator cend() const noexcept; // (1) C++20
概要
末尾要素の次を指す読み取り専用イテレータを取得する。
end()は非constなvectorオブジェクトに対してiteratorを返し、constなvectorオブジェクトに対してはconst_iteratorを返すが、cend()はconst_iteratorを返すバージョンのみが提供されている。
アルゴリズムにイテレータの組を渡す際、アルゴリズム内でデータの書き換えが起こらないというユーザーの意図を示す場合などに有用である。
戻り値
末尾要素の次を指す読み取り専用イテレータ
例外
投げない
計算量
定数時間
備考
- この関数によって返されるイテレータは、
*thisが保持するいずれの要素も参照しない。その指す先は、不正な範囲となるだろう
例
#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 ✅