std::deque::at(3)
π 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_rangeifpos >= 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, useoperator[]when you're feeling brave (or you already validated). theat()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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms