namespace std {
template <class C>
auto
end(C& c)
-> decltype(c.end()); // (1) C++11
template <class C>
constexpr auto
end(C& c)
-> decltype(c.end()); // (1) C++17
template <class C>
constexpr auto
end(C& c)
noexcept(noexcept(c.end()))
-> decltype(c.end()); // (1) C++26
template <class C>
auto
end(const C& c)
-> decltype(c.end()); // (2) C++11
template <class C>
constexpr auto
end(const C& c)
-> decltype(c.end()); // (2) C++17
template <class C>
constexpr auto
end(const C& c)
noexcept(noexcept(c.end()))
-> decltype(c.end()); // (2) C++26
template <class T, size_t N>
T*
end(T (&array)[N]); // (3) C++11
template <class T, size_t N>
constexpr T*
end(T (&array)[N]) noexcept; // (3) C++14
}
概要
範囲から、最後尾要素の次を指すイテレータを取得する。
この関数は、メンバ関数版のend()とちがい、組み込み配列に対しても使用できる。
- (1) : 非
constのコンテナの、末尾要素の次を指すイテレータを取得する - (2) :
constのコンテナの、末尾要素の次を指すイテレータを取得する - (3) : 組み込み配列の、末尾要素の次を指すポインタを取得する
戻り値
- (1) :
return c.end(); - (2) :
return c.end(); - (3) :
return array + N;
備考
この関数は、範囲for文の実装に使用される。
例
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
void print(int x)
{
std::cout << x << " ";
}
int main()
{
// コンテナ
{
std::vector<int> v = {1, 2, 3};
decltype(v)::iterator first = std::begin(v);
decltype(v)::iterator last = std::end(v);
std::for_each(first, last, print);
}
std::cout << std::endl;
// 組み込み配列
{
int ar[] = {4, 5, 6};
int* first = std::begin(ar);
int* last = std::end(ar);
std::for_each(first, last, print);
}
}
出力
1 2 3
4 5 6
バージョン
言語
- C++11
処理系
- Clang: ??
- GCC: 4.7.0 ✅
- ICC: ??
- Visual C++: ??
参照
- N2930 Range-Based For Loop Wording (Without Concepts)
- LWG2280 - begin/end for arrays should be constexpr and noexcept
- P0031R0 A Proposal to Add Constexpr Modifiers to
reverse_iterator,move_iterator,arrayand Range Access - P3016R6 Resolve inconsistencies in
begin/endforvalarrayandbraced-initializer-list