absl::Span

A non-owning view over a contiguous sequence of objects.

Synopsis

Declared in <absl/types/span.h>

template<typename T>
class Span;

Description

A Span provides a bounds-checked, std::vector-like interface over an array whose storage is owned elsewhere. It can be constructed from a pointer and length, a C array, or any contiguous container, and it must not outlive the data it refers to.

Type Aliases

NameDescription
absl_internal_is_view Tag marking Span as a view type.
const_iterator A random-access iterator over const elements.
const_pointer A pointer to a const element.
const_reference A reference to a const element.
const_reverse_iterator A reverse iterator over const elements.
difference_type A signed type used for iterator differences.
element_type The element type, including any cv-qualification.
iterator A random-access iterator over the elements.
pointer A pointer to an element.
reference A reference to an element.
reverse_iterator A reverse iterator over the elements.
size_type An unsigned type used for sizes and indices.
value_type The element type with cv-qualifiers removed.

Member Functions

NameDescription
Span [constructor]Constructors
at Returns a reference to the ith element, with bounds checking.
back Returns a reference to the last element; the span must not be empty.
begin Returns an iterator to the first element.
cbegin Returns a const iterator to the first element.
cend Returns a const iterator just past the last element.
crbegin Returns a const reverse iterator to the last element.
crend Returns a const reverse iterator just before the first element.
data Returns a pointer to the span's underlying array of data.
empty Reports whether this span is empty.
end Returns an iterator just past the last element.
first Returns a span containing the first len elements.
front Returns a reference to the first element; the span must not be empty.
last Returns a span containing the last len elements.
length Returns the length (size) of this span.
operator[] Returns a reference to the ith element of this span.
rbegin Returns a reverse iterator to the last element.
remove_prefix Removes the first n elements from the span.
remove_suffix Removes the last n elements from the span.
rend Returns a reverse iterator just before the first element.
size Returns the number of elements in this span.
subspan Returns a subspan starting at pos and spanning len elements.

Static Data Members

NameDescription
npos Sentinel size value meaning "until the end of the span".

Friends

NameDescription
absl::AbslHashValueHashes the span's elements for use with absl::Hash.

Non-Member Functions

NameDescription
MakeConstSpanConstructs a Span<const T> from a pointer and a size.
MakeConstSpanConstructs a Span<const T> from a C array.
MakeConstSpanConstructs a Span<const T> from a [begin, end)] pointer pair.
MakeSpanConstructs a mutable Span<T> from a C array.
MakeSpanConstructs a mutable Span<T> from a pointer and a size.
MakeSpanConstructs a mutable Span<T> from a [begin, end)] pointer pair.
operator!=Compares two spans for inequality.
operator!=Compares a span to a container for inequality.
operator!=Compares two spans for inequality.
operator!=Compares two spans for inequality.
operator<Compares two spans lexicographically.
operator<Compares two spans lexicographically.
operator<Compares two spans lexicographically.
operator<Compares a span and a container lexicographically.
operator<=Compares two spans lexicographically.
operator<=Compares two spans lexicographically.
operator<=Compares two spans lexicographically.
operator<=Compares a span and a container lexicographically.
operator==Compares two spans for element-wise equality.
operator==Compares two spans for element-wise equality.
operator==Compares a span to a container for element-wise equality.
operator==Compares two spans for element-wise equality.
operator>Compares two spans lexicographically.
operator>Compares two spans lexicographically.
operator>Compares two spans lexicographically.
operator>Compares a span and a container lexicographically.
operator>=Compares two spans lexicographically.
operator>=Compares two spans lexicographically.
operator>=Compares two spans lexicographically.
operator>=Compares a span and a container lexicographically.