C++ <array> at function

C++ container class <array>


Among all the ways to access array elements,atis the safest one, because it providesBounds checking。

atis a member function of container classes, used forreturns the element at the specified positionwhile performing bounds checking. If the index is out of bounds, it throwsstd::out_of_rangean exception.

atimplements safe element access, allowing you to catch and handle out-of-bounds errors at runtime.

Word Definitions: atmeans "at position", i.e., obtains the element at the specified position.


Basic syntax and parameters

atis a member function of container classes, so you need to have a container object first, then use the dot operator.to call it.

Syntax format

reference at(size_type pos);
const_reference at(size_type pos) const;

Parameter description

  • Parameter: pos
    • Type:size_type(an unsigned integer type, usuallysize_t)
    • Description: The position (index) of the element to access. The index starts at 0, and the maximum valid index issize() - 1。

Function description

  • Return Value: returns the element at the specified positionReference. If the container is a const container, a const reference is returned.
  • Effect: Returns the element at the specified position. If the index is out of the valid range (pos >= size()), it will throwstd::out_of_rangean exception.
  • difference from operator[]: atperforms bounds checking,operator[]does not perform bounds checking (out-of-bounds behavior is undefined).

Example

Let's thoroughly master it through a series of examples from simple to complex.atUsage.

Example 1: Basic usage - accessing elements

Example

#include <iostream>
#include <array>
#include <stdexcept>

int main() {
    // 1. Create an array and initialize it
    std::array<int, 5> numbers = {10, 20, 30, 40, 50};

    std::cout << "The size of array is: " << numbers.size() << std::endl;

    // 2. Use at to access elements
    std::cout << First element (at(0)): << numbers.at(0) << std::endl;
    std::cout << Second element (at(1)): << numbers.at(1) << std::endl;
    std::cout << Third element (at(2)): << numbers.at(2) << std::endl;
    std::cout << Last element (at(4)): << numbers.at(4) << std::endl;

    // 3. Use a loop to access all elements
    std::cout << "All elements: ";
    for(size_t i = 0; i < numbers.size(); ++i) {
        std::cout << numbers.at(i) << " ";
    }
    std::cout << std::endl;

    return 0;
}

Expected output:

array的大小是: 5
第一个元素 (at(0)): 10
第二个元素 (at(1)): 20
第三个元素 (at(2)): 30
最后一个元素 (at(4)): 50
所有元素: 10 20 30 40 50

Code analysis:

  1. numbers.at(0)Returns the first element10(indices start at 0).
  2. numbers.at(4)returns the last element50(becausesize()is 5, valid indices are 0-4).
  3. using loops andatcan safely traverse all elements.

Example 2: Catching out-of-bounds exceptions

atThe main advantage is that it can throw exceptions, allowing us to handle out-of-bounds errors.

Example

#include <iostream>
#include <array>
#include <stdexcept>

int main() {
    std::array<int, 3> numbers = {10, 20, 30};

    // Try to access an out-of-bounds position
    try {
        std::cout << Trying to access at(10)... << std::endl;
        int value = numbers.at(10); // This will throw an exception
        std::cout << Value: << value << std::endl;
    }
    catch(const std::out_of_range& e) {
        std::cout << Caught exception: << e.what() << std::endl;
    }

    // Normal access
    try {
        std::cout << Access at(2): << numbers.at(2) << std::endl;
    }
    catch(const std::out_of_range& e) {
        std::cout << Caught exception: << e.what() << std::endl;
    }

    return 0;
}

Expected output:

尝试访问 at(10)...
捕获到异常: array::at: __pos (which is 10) >= _Nm (which is 3)
访问 at(2): 30

Code analysis:

  • When accessingat(10)when, because the index is out of range, it throwsstd::out_of_rangean exception.
  • catch(const std::out_of_range& e)Catch exception,e.what()returns the description of the exception.
  • normal index access does not throw exceptions.

Example 3: Modifying the value of an element

atIt returns a reference, so it can be used to modify the element's value.

Example

#include <iostream>
#include <array>

int main() {
    std::array<int, 3> numbers = {10, 20, 30};

    std::cout << "Before modification: ";
    for(size_t i = 0; i < numbers.size(); ++i) {
        std::cout << numbers.at(i) << " ";
    }
    std::cout << std::endl;

    // Use at() to get a reference and modify the element
    numbers.at(0) = 100;
    numbers.at(1) = 200;
    numbers.at(2) = 300;

    std::cout << "After modification: ";
    for(size_t i = 0; i < numbers.size(); ++i) {
        std::cout << numbers.at(i) << " ";
    }
    std::cout << std::endl;

    return 0;
}

Expected output:

修改前: 10 20 30
修改后: 100 200 300

Code analysis:

  • numbers.at(0) = 100;Through the reference, the value of the first element was modified.
  • This demonstratesat()returns a modifiable lvalue reference.

C++ container class <array>

other extensions