close
The Wayback Machine - https://web.archive.org/web/20230522120026/https://en.cppreference.com/w/cpp/container/span/size
Namespaces
Variants
Views
Actions

std::span<T,Extent>::size

From cppreference.com
< cpp‎ | container‎ | span
constexpr size_type size() const noexcept;

Returns the number of elements in the span.

Contents

[edit] Parameters

(none)

[edit] Return value

The number of elements in the span.

[edit] Note

Feature-test macro
__cpp_lib_ssize

[edit] Example

#include <iostream>
#include <span>
 
void show_sizes(std::span<const int> span)
{
    std::cout
        << span                 .size() << ' ' // 8
        << span.first(7)        .size() << ' ' // 7
        << span.first<6>()      .size() << ' ' // 6 
        << span.last(5)         .size() << ' ' // 5 
        << span.last<4>()       .size() << ' ' // 4
        << span.subspan(2, 3)   .size() << ' ' // 3 
        << span.subspan<3, 2>() .size() << ' ' // 2
        << '\n';
}
 
int main()
{
    int antique_array[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    show_sizes(antique_array);
}

Output:

8 7 6 5 4 3 2

[edit] See also

constructs a span
(public member function) [edit]
returns the size of the sequence in bytes
(public member function) [edit]