std::deque::at(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::deque::at(3) β€” a C++ Standard Library moment

okay so bestie, welcome to the std::deque::at page, straight out of the C++ Standard Library (yes, that one). this is the safe-access girlie of the deque world and honestly? she ate. let me walk you through it πŸŽ€


✨ NAME ✨

std::deque::at β€” like, literally std::deque::at. that's the whole name. iconic behaviour, no notes πŸ’…


🌸 SYNOPSIS 🌸

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

so what she does is: returns a reference to the element at the specified location pos, with bounds checking πŸ”₯ that last bit is the whole personality β€” she checks first, unlike some functions we could name (cough operator[] cough).

if pos is not within the range of the container, an exception of type std::out_of_range is thrown. she will absolutely throw hands. respectfully. πŸ’€


πŸ¦‹ PARAMETERS πŸ¦‹

  • pos β€” position of the element to return. that's it. that's the param. simple queen ✨

🫢 RETURN VALUE 🫢

a reference to the requested element. you asked for it, you got it, slay πŸ’–


😭 EXCEPTIONS 😭

  • std::out_of_range if pos >= size().

like, if you go out of bounds she WILL throw std::out_of_range at you and honestly you deserved it for trying to access index 6 of a 6-element deque. boundaries, bestie. we love them 🌈


⭐ COMPLEXITY ⭐

Constant. O(1). no scrolling, no searching, no drama β€” instant. main character efficiency πŸ‘‘


🧚 EXAMPLE 🧚

// Run this code

#include <iostream>
#include <deque>
#include <stdexcept>

#ifdef __GNUG__
[[gnu::noinline]]
#endif
unsigned int runtime_six() // Emulate runtime input
{
    return 6u;
}

int main()
{
    std::deque<int> data = {1, 2, 4, 5, 5, 6};

    // Set element 1
    data.at(1) = 88;

    // Read element 2
    std::cout << "Element at index 2 has value " << data.at(2) << '\n';

    std::cout << "data size = " << data.size() << '\n';

    try
    {
        // Set element 6, where the index is determined at runtime
        data.at(runtime_six()) = 666;
    }
    catch (std::out_of_range const& exc)
    {
        std::cout << exc.what() << '\n';
    }

    // Print final values
    std::cout << "data:";
    for (int elem : data)
        std::cout << ' ' << elem;
    std::cout << '\n';
}

Possible output:

Element at index 2 has value 4
data size = 6
deque::_M_range_check: __n (which is 6)>= this->size() (which is 6)
data: 1 88 4 5 5 6

see how element 1 got set to 88? and index 2 was still 4? and then runtime_six() tried to hit index 6 and the catch block caught the std::out_of_range and printed that whole deque::_M_range_check message? yeah. that's the vibe check failing, and the code handling it gracefully. we stan a try/catch πŸ“


πŸ”₯ SEE ALSO πŸ”₯

  • operator[] β€” access specified element (public member function). she's the reckless sister. no bounds checking. no safety net. live your truth but know the risk πŸ’€

fr fr bestie: use .at() when you want the bounds check and the exception, use operator[] when you're feeling brave (or you already validated). the at() girlies stay winning πŸ†

and that's the page!! source is http://cppreference.com, dated 2024.06.10 β€” go drink some water and go touch a std::deque, ily πŸ«ΆπŸ’œπŸŽ€

πŸ“¦ where this page comes from