C++ std::array::at

The std::array::at function provides safe access to the elements of a std::array. Unlike the subscript operator ([]), at() performs bounds checking and throws an exception if the specified index is out of range. This makes it a safer alternative for accessing elements of an array.


Syntax of std::array::at

</>
Copy
reference at(size_type pos);
const_reference at(size_type pos) const;

Parameters

ParameterDescription
posThe index of the element to be accessed. The value must be within the range [0, size()-1].

Return Value

Returns a reference to the element at the specified position in the array. If the array is const, the returned reference is also const.

Exceptions

Throws a std::out_of_range exception if pos is not within the valid range of the array.


Examples for std::array::at

Example 1: Accessing Elements Using std::array::at

This example demonstrates how to access elements of a std::array using at():

</>
Copy
#include <iostream>
#include <array>

int main() {
    std::array<int, 5> arr = {10, 20, 30, 40, 50};

    std::cout << "Element at index 2: " << arr.at(2) << std::endl;
    arr.at(2) = 100; // Modify the value at index 2
    std::cout << "Modified element at index 2: " << arr.at(2) << std::endl;

    return 0;
}

Explanation:

  1. A std::array named arr is initialized with the elements {10, 20, 30, 40, 50}.
  2. The arr.at(2) function is used to access the element at index 2 (third element) in the array. It is safer than the subscript operator [] because it checks for out-of-bounds access.
  3. The program outputs the element at index 2, which is initially 30.
  4. The value at index 2 is modified using arr.at(2) = 100, updating it to 100.
  5. The program outputs the modified element at index 2, which is now 100.

Output:

Element at index 2: 30
Modified element at index 2: 100

Example 2: Handling Out-of-Range Exception

Without Exception Handling

This example shows what happens when an invalid index is accessed without exception handling:

</>
Copy
#include <iostream>
#include <array>

int main() {
    std::array<int, 3> arr = {1, 2, 3};

    std::cout << "Accessing invalid index 5: " << arr.at(5) << std::endl;

    return 0;
}

Explanation:

The program attempts to access an out-of-range index 5. This triggers a std::out_of_range exception and causes the program to terminate with an error.

Output:

terminate called after throwing an instance of 'std::out_of_range'
  what():  array::at: __n (which is 5) >= _Nm (which is 3)

With Exception Handling

This example demonstrates handling the exception using a try-catch block:

</>
Copy
#include <iostream>
#include <array>
#include <stdexcept>

int main() {
    std::array<int, 3> arr = {1, 2, 3};

    try {
        std::cout << "Accessing invalid index 5: " << arr.at(5) << std::endl;
    } catch (const std::out_of_range& e) {
        std::cerr << "Exception caught: " << e.what() << std::endl;
    }

    return 0;
}

Explanation:

  1. Access invalid index: The program attempts to access index 5, which is out of bounds.
  2. Exception handling: The exception is caught in the catch block, and an appropriate error message is displayed.
  3. Program continues: The program does not terminate unexpectedly, ensuring graceful handling of errors.

Output:

Accessing invalid index 5: Exception caught: array::at: __n (which is 5) >= _Nm (which is 3)