(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
| Member functions | ||||
| Element access | ||||
(C++26) | ||||
| Iterators | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Observers | ||||
| Subviews | ||||
span::first | ||||
| Non-member functions | ||||
| Non-member constant | ||||
| Deduction guides |
template<std::size_t Count> constexprstd::span<element_type, Count> first()const; | (1) | (since C++20) |
constexprstd::span<element_type,std::dynamic_extent> first( size_type count)const; | (2) | (since C++20) |
Obtains a subview over the firstCount orcount elements of this span.
IfCount> size() orcount> size() istrue, the behavior is undefined. | (until C++26) |
IfCount> size() orcount> size() istrue:
| (since C++26) |
Contents |
| count | - | the number of the elements of the subview |
#include <iostream>#include <ranges>#include <span>#include <string_view> void print(conststd::string_view title,const std::ranges::forward_rangeauto& container){auto size{std::size(container)};std::cout<< title<<'['<< size<<"]{";for(constauto& elem: container)std::cout<< elem<<(--size?", ":"");std::cout<<"};\n";} void run_game(std::span<constint> span){ print("span: ", span); std::span<constint,5> span_first= span.first<5>(); print("span.first<5>(): ", span_first); std::span<constint,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}; obtains a subspan consisting of the lastN elements of the sequence(public member function)[edit] | |
| obtains a subspan (public member function)[edit] |