std::vector::at(3)
π NAME π
std::vector::at β bestie's fave bounds-checked element getter, from the C++ Standard Libary β¨ (yes that's the manual name, we don't gatekeep the spelling π
)
πΈ Synopsis πΈ
reference at( size_type pos ); (1) (constexpr since C++20)
const_reference at( size_type pos ) const; (2) (constexpr since C++20)
Okay so picture this: you hand at a position pos and it hands you back a reference to the element at that exact spot β but like, with bounds checking π
it's giving responsible queen behavior. She checks first. She does not just rawdog the memory.
And if pos is not within the range of the container? She throws. Specifically, an exception of type std::out_of_range π so you literally cannot pretend you didn't get warned.
π¦ Parameters π¦
posβ the position of the element you wanna return π«Ά that's the whole vibe, that's the whole parameter.
β Return value β
A reference to the requested element. Slay. That's it, that's the return value π
π₯ Exceptions π₯
std::out_of_rangeifpos >= size()π like, she said what she said.
ngl bestie, this is the entire reason you use at instead of accidentally doing something cursed. respect the bounds or catch the throw π
π Complexity π
Constant. O(1) energy. Main character efficiency, no notes β
π Example π
// Run this code
#include <iostream>
#include <vector>
#include <stdexcept>
#ifdef __GNUG__
[[gnu::noinline]]
#endif
unsigned int runtime_six() // Emulate runtime input
{
return 6u;
}
int main()
{
std::vector<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
vector::_M_range_check: __n (which is 6) >= this->size() (which is 6)
data: 1 88 4 5 5 6
The way data.at(1) = 88 just works because it's a reference π₯ and then data.at(runtime_six()) with 6u walks straight into the std::out_of_range catch block and prints the whole exc.what() message π iconic honestly. And after all that drama, data is still 1 88 4 5 5 6 β element 2 is untouched at value 4 because index 2 holds the third element, which is 4. fr fr.
π§ See also π§
operator[]β access specified element (public member function) β the unbothered sibling who does not do bounds checking, so likeβ¦ be careful bestie, iykyk π
okay that's the whole page, you ate that π Brought to you by http://cppreference.com, dated 2024.06.10 β big kisses from your girl, now go write some bounds-checked code and catch your exceptions πβ¨π¦
π¦ 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