Przejdź do głównej zawartości

std::string_view back() method

// Const version only
constexpr const_reference back() const;

Returns a reference

to the last character in the string_view.

Undefined Behavior

Calling back() on an empty string_view is undefined behavior

.

Parameters

(none)

Return value

Reference to the last character.

Complexity

Constant - O(1).

Notes

For a string_view str, the expression str.back() is equivalent to *std::prev(str.end()) and str[str.size() - 1].

Example

Main.cpp
#include <string_view>
#include <iostream>

int main() {
for (std::string_view 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.

std::string_view back() method

// Const version only
constexpr const_reference back() const;

Returns a reference

to the last character in the string_view.

Undefined Behavior

Calling back() on an empty string_view is undefined behavior

.

Parameters

(none)

Return value

Reference to the last character.

Complexity

Constant - O(1).

Notes

For a string_view str, the expression str.back() is equivalent to *std::prev(str.end()) and str[str.size() - 1].

Example

Main.cpp
#include <string_view>
#include <iostream>

int main() {
for (std::string_view 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.