# std::span<T,Extent>::operator[]

> C++ Atlas: `cppatlas.com/w/cpp/container/span/operator_at` | Source: https://en.cppreference.com/w/cpp/container/span/operator_at | Domain: Containers | Standards: C++20, C++26

Returns a reference to the idxth element of the sequence. The behavior is undefined if idx is out of range (i.e., if it is greater than or equal to size()).

## Official Specification Summary (cppreference)

```cpp
constexpr reference operator[]( size_type idx ) const; // (since C++20)
```

Returns a reference to the `idx`th element of the sequence. The behavior is undefined if `idx` is out of range (i.e., if it is greater than or equal to `size()`).

### 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]`.

### Exceptions

Throws nothing.

### Example

```cpp
#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:

```text
1 2 3 4 5
5 4 3 2 1
```

### See also

- [`at`](https://en.cppreference.com/w/cpp/container/span/at) (C++26): access specified element with bounds checking (public member function)
- [`data`](https://en.cppreference.com/w/cpp/container/span/data): direct access to the underlying contiguous storage (public member function)
- [`size`](https://en.cppreference.com/w/cpp/container/span/size): returns the number of elements (public member function)
- [`as_bytes`](https://en.cppreference.com/w/cpp/container/span/as_bytes) (C++20), [`as_writable_bytes`](https://en.cppreference.com/w/cpp/container/span/as_bytes): converts a `span` into a view of its underlying bytes (function template)

---
Attribution: content from https://en.cppreference.com/w/cpp/container/span/operator_at (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.