std::span back() method
- od C++20
constexpr reference back() const;
Returns a reference
to the last character in the span.Undefined Behavior
Calling back()
on an empty span is undefined behavior
Parameters
(none)
Return value
Reference to the last element.
Complexity
Constant - O(1).
Notes
For a span s
, the expression s.back()
is equivalent to *std::prev(s.end())
and s[s.size() - 1]
.
Example
Main.cpp
#include <span>
#include <iostream>
int main() {
for (std::span str{"ABCDEF"}; !str.empty(); str.remove_suffix(1))
std::cout << str.back() << ' ' << str << '\n';
}
Output
F ABCDEF
E ABCDE
D ABCD
C ABC
B AB
A A
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.
Hover to see the original license.