Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/span/first"

From cppreference.com
< cpp‎ | container‎ | span
(E: simplification (now span::last() has its own Example))
m (Example: -',')
 
(7 intermediate revisions by 4 users not shown)
Line 2: Line 2:
 
{{cpp/container/span/navbar}}
 
{{cpp/container/span/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | 1=
+
{{dcl|since=c++20|1=
 
template< std::size_t Count >
 
template< std::size_t Count >
 
constexpr std::span<element_type, Count> first() const;
 
constexpr std::span<element_type, Count> first() const;
 
}}
 
}}
{{dcl | 1=
+
{{dcl|since=c++20|1=
constexpr std::span<element_type, std::dynamic_extent> first( std::size_t Count ) const;
+
constexpr std::span<element_type, std::dynamic_extent> first( size_type Count ) const;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 13: Line 13:
 
Obtains a span that is a view over the first {{tt|Count}} elements of this span. The program is ill-formed if {{c|Count > Extent}}. The behavior is undefined if {{c|Count > size()}}.
 
Obtains a span that is a view over the first {{tt|Count}} elements of this span. The program is ill-formed if {{c|Count > Extent}}. The behavior is undefined if {{c|Count > size()}}.
  
=== Return value ===
+
===Return value===
 
A span {{tt|r}} that is a view over the first {{tt|Count}} elements of {{c|*this}}, such that {{c|1=r.data() == this->data() && r.size() == Count}}.
 
A span {{tt|r}} that is a view over the first {{tt|Count}} elements of {{c|*this}}, such that {{c|1=r.data() == this->data() && r.size() == Count}}.
  
 
===Example===
 
===Example===
{{example |
+
{{example
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 +
#include <ranges>
 
#include <span>
 
#include <span>
 
#include <string_view>
 
#include <string_view>
  
void print(std::string_view const title,  
+
void print(std::string_view const title,
           /* std::ranges::forward_range */ auto const& container) {
+
           std::ranges::forward_range auto const& container)
     std::cout << title << "[" << std::size(container) << "]{ ";
+
{
 +
    auto size{std::size(container)};
 +
     std::cout << title << '[' << size << "]{";
 
     for (auto const& elem : container)
 
     for (auto const& elem : container)
         std::cout << elem << ", ";
+
         std::cout << elem << (--size ? ", " : "");
 
     std::cout << "};\n";
 
     std::cout << "};\n";
 
}
 
}
Line 39: Line 42:
  
 
     std::span<const int, std::dynamic_extent> span_first_dynamic = span.first(4);
 
     std::span<const int, std::dynamic_extent> span_first_dynamic = span.first(4);
     print("span.first(4):   ", span_first_dynamic);
+
     print("span.first(4): ", span_first_dynamic);
 
}
 
}
  
 
int main()
 
int main()
 
{
 
{
     int a[8]{ 1,2,3,4,5,6,7,8 };
+
     int a[8]{1, 2, 3, 4, 5, 6, 7, 8};
     print("int a", a);
+
     print("int a", a);
 
     run_game(a);
 
     run_game(a);
 
}
 
}
| output=
+
|output=
int a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, };
+
int a[8]{1, 2, 3, 4, 5, 6, 7, 8};
span: [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<5>(): [5]{1, 2, 3, 4, 5};
span.first(4):   [4]{ 1, 2, 3, 4, };
+
span.first(4): [4]{1, 2, 3, 4};
 
}}
 
}}
  
=== See also ===
+
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/container/span/dsc last}}
+
{{dsc inc|cpp/container/span/dsc last}}
{{dsc inc | cpp/container/span/dsc subspan}}
+
{{dsc inc|cpp/container/span/dsc subspan}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 02:44, 2 October 2023

 
 
 
 
template< std::size_t Count >
constexpr std::span<element_type, Count> first() const;
(since C++20)
constexpr std::span<element_type, std::dynamic_extent> first( size_type Count ) const;
(since C++20)

Obtains a span that is a view over the first Count elements of this span. The program is ill-formed if Count > Extent. The behavior is undefined if Count > size().

[edit] Return value

A span r that is a view over the first Count elements of *this, such that r.data() == this->data() && r.size() == Count.

[edit] Example

#include <iostream>
#include <ranges>
#include <span>
#include <string_view>
 
void print(std::string_view const title,
           std::ranges::forward_range auto const& container)
{
    auto size{std::size(container)};
    std::cout << title << '[' << size << "]{";
    for (auto const& 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);
}

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};

[edit] See also

obtains a subspan consisting of the last N elements of the sequence
(public member function) [edit]
obtains a subspan
(public member function) [edit]