std::deque::operator[](3)

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

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, at is 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