Przejdź do głównej zawartości

std::span size() method

constexpr size_type size() const noexcept;

Returns the number of elements in the span, i.e. std::distance(begin(), end()).

Parameters

(none)

Return value

The number of elements in the span.

Complexity

Constant - O(1).

Notes

Feature testing macro: __cpp_lib_ssize.

Example

Main.cpp
#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);
}
Possible output
8 7 6 5 4 3 2 
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::span size() method

constexpr size_type size() const noexcept;

Returns the number of elements in the span, i.e. std::distance(begin(), end()).

Parameters

(none)

Return value

The number of elements in the span.

Complexity

Constant - O(1).

Notes

Feature testing macro: __cpp_lib_ssize.

Example

Main.cpp
#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);
}
Possible output
8 7 6 5 4 3 2 
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.