function template
<iterator> <array> <deque> <forward_list> <list> <map> <regex> <set> <string> <unordered_map> <unordered_set> <vector>
std::begin
container (1) | template <class Container> auto begin (Container& cont) -> decltype (cont.begin());template <class Container> auto begin (const Container& cont) -> decltype (cont.begin()); |
---|
array (2) | template <class T, size_t N> T* begin (T(&arr)[N]); |
---|
container (1) | template <class Container> auto begin (Container& cont) -> decltype (cont.begin());template <class Container> auto begin (const Container& cont) -> decltype (cont.begin()); |
---|
array (2) | template <class T, size_t N> constexpr T* begin (T(&arr)[N]) noexcept; |
---|
Iterator to beginning
Returns an iterator pointing to the first element in the sequence:
- (1) Container
- The function returns cont.begin().
- (2) Array
- The function returns the array-to-pointer conversion of its argument.
If the sequence is empty, the returned value shall not be dereferenced.
These function templates are defined in multiple headers: Each of these headers includes the generic templates for all container and array types and not simply a specific overload. The headers are: <iterator>, <array>, <deque>, <forward_list>, <list>, map, <regex>, <set>, <string>, <unordered_map>, <unordered_set> and <vector>.
Conversely, begin is overloaded (with a different definition) in headers <initializer_list>
and <valarray>
.
Parameters
- cont
- An object of a class type for which member begin is defined.
- arr
- An array.
Return Value
For (1), the same as returned by cont.begin().
For (2), a pointer to the first element in the array.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// std::begin / std::end example
#include <iostream> // std::cout
#include <vector> // std::vector, std::begin, std::end
int main () {
int foo[] = {10,20,30,40,50};
std::vector<int> bar;
// iterate foo: inserting into bar
for (auto it = std::begin(foo); it!=std::end(foo); ++it)
bar.push_back(*it);
// iterate bar: print contents:
std::cout << "bar contains:";
for (auto it = std::begin(bar); it!=std::end(bar); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
Output:
bar contains: 10 20 30 40 50
|
Data races
The argument is accessed but not modified.
None of the elements in the sequence are accessed by the call, but the iterator returned can be used to access or modify them.
Exception safety
Provides the same level of guarantees as the operation performed on the argument (for standard containers and arrays this is a no-throw guarantee).
See also
- end
- Iterator to end (function template)