std::array::operator[](3)
π
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
- 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