Skip to main content

std::span first() method

// (1) Const version only
template< std::size_t Count >
constexpr std::span<element_type, Count> first() const;

// (2) Const version only
constexpr std::span<element_type, std::dynamic_extent> first( size_type Count ) const;

Obtains a span that is a view over the first Count elements of this span.

important

The program is ill-formed

if Count > Extent.

Undefined Behaviour

The behavior is undefined

if Count > size().

Parameters

  • (2) - Count - the number of the elements to make a span of

Return value

A span s that is a view over the first Count elements of *this, with the following properties:

  • s.data() == this->data()
  • s.size() == Count

Complexity

Constant - O(1).

Exceptions

(none)

Example

Main.cpp
#include <iostream>
#include <span>
#include <string_view>

void print(std::string_view const title,
/* std::ranges::forward_range */ auto const& container) {
std::cout << title << "[" << std::size(container) << "]{ ";
for (auto const& elem : container)
std::cout << elem << ", ";
std::cout << "};\n";
}

void run_game(std::span<const int> span)
{
print("span: ", span);

std::span<const int, 5> span_first = span.first<5>();
print("span.first<5>(): ", span_first);

std::span<const int, std::dynamic_extent> span_first_dynamic = span.first(4);
print("span.first(4): ", span_first_dynamic);
}

int main()
{
int a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
print("int a", a);
run_game(a);
}
Output
int  a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
span: [8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
span.first<5>(): [5]{ 1, 2, 3, 4, 5, };
span.first(4): [4]{ 1, 2, 3, 4, };
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 first() method

// (1) Const version only
template< std::size_t Count >
constexpr std::span<element_type, Count> first() const;

// (2) Const version only
constexpr std::span<element_type, std::dynamic_extent> first( size_type Count ) const;

Obtains a span that is a view over the first Count elements of this span.

important

The program is ill-formed

if Count > Extent.

Undefined Behaviour

The behavior is undefined

if Count > size().

Parameters

  • (2) - Count - the number of the elements to make a span of

Return value

A span s that is a view over the first Count elements of *this, with the following properties:

  • s.data() == this->data()
  • s.size() == Count

Complexity

Constant - O(1).

Exceptions

(none)

Example

Main.cpp
#include <iostream>
#include <span>
#include <string_view>

void print(std::string_view const title,
/* std::ranges::forward_range */ auto const& container) {
std::cout << title << "[" << std::size(container) << "]{ ";
for (auto const& elem : container)
std::cout << elem << ", ";
std::cout << "};\n";
}

void run_game(std::span<const int> span)
{
print("span: ", span);

std::span<const int, 5> span_first = span.first<5>();
print("span.first<5>(): ", span_first);

std::span<const int, std::dynamic_extent> span_first_dynamic = span.first(4);
print("span.first(4): ", span_first_dynamic);
}

int main()
{
int a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
print("int a", a);
run_game(a);
}
Output
int  a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
span: [8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
span.first<5>(): [5]{ 1, 2, 3, 4, 5, };
span.first(4): [4]{ 1, 2, 3, 4, };
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.