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

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

πŸ’… std::array::operator[](3) πŸ’…

okay so bestie, this iconic page is from the C++ Standard Libary manual and it’s all about std::array::operator[] β€” std::array::operator[]. Get ready for bracket access realness. πŸ’–


🌸 NAME 🌸

std::array::operator[] β€” std::array::operator[]

✨ Synopsis ✨

reference operator[]( size_type pos );             (1) (since C++11)
                                                   (constexpr since C++17)
const_reference operator[]( size_type pos ) const; (2) (since C++11)
                                                   (constexpr since C++14)

Returns a reference to the element at specified location pos. No bounds checking is performed. periodt.

bestie, that β€œno bounds checking” thing is real β€” it’s not checking, it’s just vibing. πŸ‘€

πŸŽ€ Parameters πŸŽ€

  • pos β€” position of the element to return.

🫢 Return value 🫢

Reference to the requested element.

⚑ Complexity ⚑

Constant. iconic, no notes. ✨

πŸ’œ Notes πŸ’œ

Unlike std::map::operator[], this operator never inserts a new element into the container. No sneaky insertion, no new element, nada.

Accessing a nonexistent element through this operator is undefined behavior. So like, do not do that, fr fr πŸ˜­πŸ’€.

🌈 Example 🌈

The following code uses operator[] to read from and write to a std::array<int>:

// Run this code

#include <array>
#include <iostream>

int main()
{
    std::array<int,4> 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).

kisses bestie πŸ’‹ this page is sourced from http://cppreference.com, version/date 2024.06.10, and it’s std::array::operator[](3) β€” bye πŸ’–βœ¨

πŸ“¦ where this page comes from