std::deque::operator[](3)
okay so bestie, welcome to the <pink>std::deque::operator[]</pink> page in section (3) of the C++ Standard Libary manual. Yes, the header spells it βLibaryβ β we're keeping that exactly as-is, and honestly? iconic. π
πΈ NAME πΈ
std::deque::operator[] - std::deque::operator[]
That's the name, bestie. It's giving βthe bracket-bracket operator of the deque.β π
π Synopsis π
reference operator[]( size_type pos ); (1)
const_reference operator[]( size_type pos ) const; (2)
Okay so these signatures return a reference to the element at specified location pos. And no bounds checking is performed. Not a little, not a tiny bit β none. π
π― Parameters π―
posβ position of the element to return. That's the whole parameter. Slay. β¨
π«Ά Return value π«Ά
Reference to the requested element. That's it, that's the return value. π
β‘ Complexity β‘
Constant. That is the entire complexity section, and it ate. β
π Notes π
- Unlike
std::map::operator[], this operator never inserts a new element into the container. Not ever. π ββοΈ - Accessing a nonexistent element through this operator is undefined behavior. Like, do not do it, bestie. That's the warning. π
π Example π
The following code uses operator[] to read from and write to a std::deque<int>:
// Run this code
#include <deque>
#include <iostream>
int main()
{
std::deque<int> numbers{2, 4, 6, 8};
std::cout << "Second element: " << numbers[1] << '\n';
numbers[0] = 5;
std::cout << "All numbers:";
for (auto i : numbers)
std::cout << ' ' << i;
std::cout << '\n';
}
β¨ Output β¨
Second element: 4
All numbers: 5 4 6 8
π See also π
atβ access specified element with bounds checking (public member function). Yeah,atis the one doing the bounds checking. π
mwah π β source/version and date: http://cppreference.com, 2024.06.10. Bye bestie, go index responsibly! ππ¦
π¦ 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