std::span::last(3)
π std::span::last(3) π
okay bestie, welcome to the C++ Standard Libary page for std::span::last β yes, βLibaryβ is canon in the header, weβre leaving it iconic π
. This is the whole vibe: grabbing a view of the last Count elements of a span. No cap, itβs giving βtail of the group chatβ energy.
π NAME π
std::span::last β std::span::last
Yes, thatβs literally the name. The function is std::span::last, and this page is aboutβ¦ std::span::last. Redundant? Maybe. Iconic? Absolutely. π
πΈ Synopsis πΈ
Here are the overloads, bestie β both since C++20:
template< std::size_t Count >
(1) (since C++20)
constexpr std::span<element_type, Count> last() const;
constexpr std::span<element_type, std::dynamic_extent> last(
(2) (since C++20)
size_type Count ) const;
So whatβs the tea? last() obtains a span that is a view over the last Count elements of this span. π«Ά
π¨ ifCount > Extent, the program is ill-formed β not a vibe. And ifCount > size(), the behavior is undefined. So maybe donβt test fate, fr fr. π
π Return value π
You get 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
Thatβs the exact contract, no cap. β¨
π¦ Example π¦
Run this code, bestie:
// 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: π»
It prints exactly this:
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 π
firstβ obtains a subspan consisting of the firstNelements of the sequence (public member function)subspanβ obtains a subspan (public member function)
thatβs the whole page, bestie π β source: http://cppreference.com Β· date: 2024.06.10 Β· page: std::span::last(3) Β· now go take the last() of something cute β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms