Element access

suggest change

\1. at(pos)

Returns a reference to the element at position pos with bounds checking. If pos is not within the range of the container, an exception of type std::out_of_range is thrown.

The complexity is constant O(1).

#include <array>

int main()
{
    std::array<int, 3> arr;

    // write values
    arr.at(0) = 2;
    arr.at(1) = 4;
    arr.at(2) = 6;
        
    // read values
    int a = arr.at(0); // a is now 2
    int b = arr.at(1); // b is now 4
    int c = arr.at(2); // c is now 6

    return 0;
}

\2) operator[pos]

Returns a reference to the element at position pos without bounds checking. If pos is not within the range of the container, the behavior is undefined. This method provides element access equivalent to classic arrays and is therefore more efficient than at(pos).

The complexity is constant O(1).

#include <array>

int main()
{
    std::array<int, 3> arr;

    // write values
    arr[0] = 2;
    arr[1] = 4;
    arr[2] = 6;
        
    // read values
    int a = arr[0]; // a is now 2
    int b = arr[1]; // b is now 4
    int c = arr[2]; // c is now 6

    return 0;
}

\3) std::get<pos>

This non-member function returns a reference to the element at compile-time constant position pos. It does not check the index at run time. If pos is not within the range of the container, the program is ill-formed.

The complexity is constant O(1).

#include <array>

int main()
{
    std::array<int, 3> arr;

    // write values
    std::get<0>(arr) = 2;
    std::get<1>(arr) = 4;
    std::get<2>(arr) = 6;
        
    // read values
    int a = std::get<0>(arr); // a is now 2
    int b = std::get<1>(arr); // b is now 4
    int c = std::get<2>(arr); // c is now 6

    return 0;
}

\4) front()

Returns a reference to the first element in the container. Calling front() on an empty container is undefined behavior.

The complexity is constant O(1).

Note: For a container c, the expression c.front() is equivalent to *c.begin().

#include <array>

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

    int a = arr.front(); // a is now 2

    return 0;
}

\5) back()

Returns a reference to the last element in the container. Calling back() on an empty container is undefined behavior.

The complexity is constant O(1).

#include <array>

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

    int a = arr.back(); // a is now 6

    return 0;
}

\6) data()

Returns a pointer to the underlying array serving as element storage. The pointer is such that the range [data(), data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).

The complexity is constant O(1).

#include <iostream>
#include <cstring>
#include <array>

int main ()
{
    const char* cstr = "Test string";
    std::array<char, 12> arr;
    
    std::memcpy(arr.data(), cstr, 12); // copy cstr to arr
    
    std::cout << arr.data(); // outputs: Test string
    
    return 0;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents