std::span<T,Extent>::last - cppreference.com (original) (raw)
Obtains a subview over the last Count or count elements of this span.
- The element count is provided as a template argument, and the subview has a static extent.
If Count > Extent is true, the program is ill-formed.
- The element count is provided as a function argument, and the subview has a dynamic extent.
If Count > size() or count > size() is true, the behavior is undefined. | (until C++26) |
---|---|
If Count > size() or count > size() is true: If the implementation is hardened, a contract violation occurs. Moreover, if the contract-violation handler returns under “observe” evaluation semantic, the behavior is undefined. If the implementation is not hardened, the behavior is undefined. | (since C++26) |
[edit] Parameters
count | - | the number of the elements of the subview |
---|
[edit] Return value
- std::span<element_type, Count>{data() + (size() - Count), Count}
[edit] Example
#include #include #include void println(const std::string_view title, const auto& container) { std::cout << title << '[' << std::size(container) << "]{ "; for (const auto& elem : container) std::cout << elem << ", "; std::cout << "};\n"; }; void run(std::span 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) [edit] | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | | obtains a subspan (public member function) [edit] |