跳到主要内容

std::string_view front() 方法

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

返回对指定索引pos处元素的引用

指向视图中的第一个字符。

未定义行为

在空视图上调用 front() 会导致未定义行为

.

参数

(无)

返回值

指向第一个字符的引用。

复杂度

常数 - O(1)

备注

对于视图 str,表达式 str.front() 等效于 *str.begin()str[0]

示例

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';
}
输出
A ABCDEF
B BCDEF
C CDEF
D DEF
E EF
F F
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而有所改动。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::string_view front() 方法

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

返回对指定索引pos处元素的引用

指向视图中的第一个字符。

未定义行为

在空视图上调用 front() 会导致未定义行为

.

参数

(无)

返回值

指向第一个字符的引用。

复杂度

常数 - O(1)

备注

对于视图 str,表达式 str.front() 等效于 *str.begin()str[0]

示例

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';
}
输出
A ABCDEF
B BCDEF
C CDEF
D DEF
E EF
F F
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而有所改动。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。