Difference between revisions of "cpp/container/span/last"
From cppreference.com
m (link to zh) |
m (→Example: fmt2) |
||
(12 intermediate revisions by 7 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|num=1|1= |
− | template< std:: | + | template< std::size_t Count > |
constexpr std::span<element_type, Count> last() const; | constexpr std::span<element_type, Count> last() const; | ||
}} | }} | ||
− | {{dcl | 1= | + | {{dcl|since=c++20|num=2|1= |
− | constexpr std::span<element_type, std::dynamic_extent> last( | + | constexpr std::span<element_type, std::dynamic_extent> last( size_type Count ) const; |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | Obtains a span that is a view over the last {{ | + | Obtains a span that is a view over the last {{c|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 last {{ | + | A span {{tt|r}} that is a view over the last {{c|Count}} elements of {{c|*this}}, such that {{c|1=r.data() == this->data() + (this->size() - Count) && r.size() == Count}}. |
− | === See also === | + | ===Example=== |
+ | {{example | ||
+ | |code= | ||
+ | #include <iostream> | ||
+ | #include <span> | ||
+ | #include <string_view> | ||
+ | |||
+ | void println(std::string_view const title, auto const& container) | ||
+ | { | ||
+ | std::cout << title << '[' << std::size(container) << "]{ "; | ||
+ | for (auto const& elem : container) | ||
+ | std::cout << elem << ", "; | ||
+ | std::cout << "};\n"; | ||
+ | }; | ||
+ | |||
+ | void run(std::span<const int> span) | ||
+ | { | ||
+ | println("span: ", span); | ||
+ | |||
+ | std::span<const int, 3> span_last = span.last<3>(); | ||
+ | println("span.last<3>(): ", span_last); | ||
+ | |||
+ | std::span<const int, std::dynamic_extent> span_last_dynamic = span.last(2); | ||
+ | println("span.last(2): ", span_last_dynamic); | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int a[8]{1, 2, 3, 4, 5, 6, 7, 8}; | ||
+ | println("int a", a); | ||
+ | run(a); | ||
+ | } | ||
+ | |output= | ||
+ | int a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, }; | ||
+ | span: [8]{ 1, 2, 3, 4, 5, 6, 7, 8, }; | ||
+ | span.last<3>(): [3]{ 6, 7, 8, }; | ||
+ | span.last(2): [2]{ 7, 8, }; | ||
+ | }} | ||
+ | |||
+ | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/container/span/dsc first}} | + | {{dsc inc|cpp/container/span/dsc first}} |
− | {{dsc inc | cpp/container/span/dsc subspan}} | + | {{dsc inc|cpp/container/span/dsc subspan}} |
{{dsc end}} | {{dsc end}} | ||
− | {{langlinks|zh}} | + | {{langlinks|de|es|ja|ru|zh}} |
Latest revision as of 07:36, 5 November 2023
template< std::size_t Count > constexpr std::span<element_type, Count> last() const; |
(1) | (since C++20) |
constexpr std::span<element_type, std::dynamic_extent> last( size_type Count ) const; |
(2) | (since C++20) |
Obtains a span that is a view over the last 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 last Count elements of *this, such that r.data() == this->data() + (this->size() - Count) && r.size() == Count.
[edit] Example
Run this code
#include <iostream> #include <span> #include <string_view> void println(std::string_view const title, auto const& container) { std::cout << title << '[' << std::size(container) << "]{ "; for (auto const& elem : container) std::cout << elem << ", "; std::cout << "};\n"; }; void run(std::span<const int> span) { println("span: ", span); std::span<const int, 3> span_last = span.last<3>(); println("span.last<3>(): ", span_last); std::span<const int, std::dynamic_extent> span_last_dynamic = span.last(2); println("span.last(2): ", span_last_dynamic); } int main() { int a[8]{1, 2, 3, 4, 5, 6, 7, 8}; println("int a", a); run(a); }
Output:
int a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, }; span: [8]{ 1, 2, 3, 4, 5, 6, 7, 8, }; span.last<3>(): [3]{ 6, 7, 8, }; span.last(2): [2]{ 7, 8, };
[edit] See also
obtains a subspan consisting of the first N elements of the sequence (public member function) | |
obtains a subspan (public member function) |