CKTIS / C++
std::begin: the start iterator of a container or array
std::begin provides one way to obtain a container’s first iterator or a pointer to the first element of a built-in array.
Version: C++23
Syntax
template<class C>
constexpr auto begin(C& container) -> decltype(container.begin());
template<class C>
constexpr auto begin(const C& container) -> decltype(container.begin());
template<class T, std::size_t N>
constexpr T* begin(T (&array)[N]) noexcept;std::begin(range) from <iterator> calls range.begin() for a container while preserving constness, and for a built-in T[N] array returns a T* to its first element. It does not dereference the range and also works when the range is empty.
In generic code, using std::begin; begin(x); introduces the standard free-function overloads into the overload set, while the unqualified call lets ADL add a user type’s function. Built-in arrays such as int[] have no begin() member at all, and ADL alone does not find the overload in std for them, so the using-declaration matters.
For an empty range the result equals std::end(range) and must not be dereferenced. An iterator obtained from a temporary container can dangle after the full expression; std::begin provides no lifetime protection. Later container operations may also invalidate it according to that container’s rules.
Examples
#include <array>
#include <iostream>
#include <iterator>
int main() {
const std::array values{2, 5};
std::cout << *std::begin(values) << '\n';
}
Result
2
std::begin → 2