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

</>
Copy
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():

</>
Copy
#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:

  1. Include necessary headers:
    • <iostream>: Used for input/output operations.
    • <array>: Includes the std::array container class.
  2. Define a std::array:
    • The program defines a std::array named arr with a size of 5 and initializes it with the elements {10, 20, 30, 40, 50}.
  3. Access array elements using std::array::begin:
    • The function std::array::begin returns an iterator pointing to the first element of the array.
    • In this program, arr.begin() is used to obtain the starting point for iteration.
  4. 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 until arr.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:

</>
Copy
#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:

  1. Purpose of std::for_each:
    • The function std::for_each is part of the C++ Standard Library and is included in the <algorithm> header.
  2. 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.