C++ std::array::begin
The std::array::begin function returns an iterator pointing to the first element of the array. This allows you to traverse the elements of the array using iterators.
Syntax of std::array::begin
iterator begin() noexcept;
const_iterator begin() const noexcept;
Parameters
The std::array::begin function does not take any parameters.
Return Value
Returns an iterator (or a constant iterator for const arrays) to the first element of the array. If the array is empty, the returned iterator will be equal to std::array::end.
Examples for std::array::begin
Example 1: Traversing an Array Using std::array::begin
This example demonstrates iterating through the elements of a std::array using begin() and end():
#include <iostream>
#include <array>
int main() {
std::array<int, 5> arr = {10, 20, 30, 40, 50};
std::cout << "Array elements: ";
for (auto it = arr.begin(); it != arr.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- Include necessary headers:
<iostream>: Used for input/output operations.<array>: Includes thestd::arraycontainer class.
- Define a
std::array:- The program defines a
std::arraynamedarrwith a size of 5 and initializes it with the elements{10, 20, 30, 40, 50}.
- The program defines a
- Access array elements using
std::array::begin:- The function
std::array::beginreturns an iterator pointing to the first element of the array. - In this program,
arr.begin()is used to obtain the starting point for iteration.
- The function
- Iterate through the array:
- A for loop is used to iterate through the array using iterators.
- The loop starts from
arr.begin()(the first element) and continues untilarr.end()(one past the last element). - Each element is accessed using the dereference operator (
*it) and printed to the console.
Output:
Array elements: 10 20 30 40 50
Key Point: The std::array::begin function provides an easy and safe way to access the starting point of an array when using iterators for traversal.
Example 2: Using std::array::begin with Standard Algorithms
This example demonstrates using std::array::begin with the std::for_each algorithm:
#include <iostream>
#include <array>
#include <algorithm>
void print_element(int x) {
std::cout << x << " ";
}
int main() {
std::array<int, 5> arr = {1, 2, 3, 4, 5};
std::cout << "Array elements: ";
std::for_each(arr.begin(), arr.end(), print_element);
std::cout << std::endl;
return 0;
}
Explanation:
- Purpose of
std::for_each:- The function
std::for_eachis part of the C++ Standard Library and is included in the<algorithm>header.
- The function
- Arguments passed to
std::for_each:arr.begin(): An iterator pointing to the first element of the array.arr.end(): An iterator pointing to one past the last element of the array.print_element: A function or callable object (such as a lambda function) that is executed for each element in the range.
Output:
Array elements: 1 2 3 4 5
Exception Handling in std::array::begin
The std::array::begin function does not throw exceptions as it is marked noexcept. It is a guaranteed safe operation.
