std::span::operator[](3)
β¨ NAME β¨
Okay bestie, this is the page for std::span::operator[] β title and section number and all: std::span::operator[](3) β from the C++ Standard Libary. Yes, βLibaryβ is literally how the manual name is spelled on this page, and I am not fixing it. π
std::span::operator[] - std::span::operator[]
πΈ SYNOPSIS πΈ
constexpr reference operator[]( size_type idx ) const; (since C++20)
Returns a reference to the idx-th element of the sequence. Cute and simple. π«Ά
π¨ UB ALERT: the behavior is undefined ifidxis out of range (i.e., if it is greater than or equal tosize()). Not a vibe, no cap, do not do this. π
π PARAMETERS π
idxβ the index of the element to access
π RETURN VALUE π
A reference to the idx-th element of the sequence, i.e., data()[idx]. Thatβs the whole gift. π
π EXCEPTIONS π
Throws nothing. Literally nothing. No exception drama here. Periodt.
π§ EXAMPLE π§
// Run this code π
#include <cstddef>
#include <iostream>
#include <span>
#include <utility>
void reverse(std::span<int> span)
{
for (std::size_t i = 0, j = std::size(span); i < j; ++i)
{
--j;
std::swap(span[i], span[j]);
}
}
void print(std::span<const int> const span)
{
for (int element : span)
std::cout << element << ' ';
std::cout << '\n';
}
int main()
{
int data[]{1, 2, 3, 4, 5};
print(data);
reverse(data);
print(data);
}
π₯οΈ OUTPUT
1 2 3 4 5
5 4 3 2 1
π SEE ALSO π
atβ access specified element with bounds checking (public member function) (C++26)dataβ direct access to the underlying contiguous storage (public member function)sizeβ returns the number of elements (public member function)as_bytesβ converts a span into a view of its underlying bytesas_writable_bytesβ (function template) (C++20)
Source/version/date sign-off, because the footer is iconic too: this page is from http://cppreference.com, version 2024.06.10, and itβs std::span::operator[](3). Bye bestie β go access some elements, but keep idx in range πβ¨π
π¦ 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