std::span<T,Extent>:: first
|
template
<
std::
size_t
Count
>
constexpr std:: span < element_type, Count > first ( ) const ; |
(1) | (desde C++20) |
|
constexpr
std::
span
<
element_type,
std::
dynamic_extent
>
first ( size_type count ) const ; |
(2) | (desde C++20) |
Obtiene una subvista sobre los primeros Count o count elementos de este span.
|
Si Count > size ( ) o count > size ( ) es true , el comportamiento es indefinido. |
(hasta C++26) |
|
Si Count > size ( ) o count > size ( ) es true :
|
(desde C++26) |
Contenidos |
Parámetros
| count | - | el número de elementos de la subvista |
Valor de retorno
Ejemplo
#include <iostream> #include <ranges> #include <span> #include <string_view> void print(const std::string_view title, const std::ranges::forward_range auto& container) { auto size{std::size(container)}; std::cout << title << '[' << size << "]{"; for (const auto& elem : container) std::cout << elem << (--size ? ", " : ""); 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); }
Salida:
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};
Véase también
obtiene un subspan que consiste en los últimos
N
elementos de la secuencia
(función miembro pública) |
|
|
obtiene un subspan
(función miembro pública) |