Przejdź do głównej zawartości

std::string_view front() method

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

Returns a reference

to the first character in the view.

Undefined Behavior

Calling front() on an empty view is undefined behavior

.

Parameters

(none)

Return value

Reference to the first character.

Complexity

Constant - O(1).

Notes

For a view str, the expression str.front() is equivalent to *str.begin() and str[0].

Example

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

int main() {
for (std::string_view str{"ABCDEF"}; !str.empty(); str.remove_prefix(1))
std::cout << str.front() << ' ' << str << '\n';
}
Output
A ABCDEF
B BCDEF
C CDEF
D DEF
E EF
F F
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 front() method

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

Returns a reference

to the first character in the view.

Undefined Behavior

Calling front() on an empty view is undefined behavior

.

Parameters

(none)

Return value

Reference to the first character.

Complexity

Constant - O(1).

Notes

For a view str, the expression str.front() is equivalent to *str.begin() and str[0].

Example

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

int main() {
for (std::string_view str{"ABCDEF"}; !str.empty(); str.remove_prefix(1))
std::cout << str.front() << ' ' << str << '\n';
}
Output
A ABCDEF
B BCDEF
C CDEF
D DEF
E EF
F F
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.