命名空间
变体
操作

std::basic_string<CharT,Traits,Allocator>::front

来自 cppreference.cn
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
CharT& front();
(1) (constexpr since C++20)
const CharT& front() const;
(2) (constexpr since C++20)

返回字符串中首字符的引用。如果 empty()true,则行为未定义。

内容

[编辑] 参数

(无)

[编辑] 返回值

首字符的引用,等效于 operator[](0)

[编辑] 复杂度

常数复杂度。

[编辑] 注解

在 libstdc++ 中,C++98 模式下 不提供 front()

[编辑] 示例

#include <iostream>
#include <string>
 
int main()
{
    std::string s("Exemplary");
    char& f1 = s.front();
    f1 = 'e';
    std::cout << s << '\n'; // "exemplary"
 
    std::string const c("Exemplary");
    char const& f2 = c.front();
    std::cout << &f2 << '\n'; // "Exemplary"
}

输出

exemplary
Exemplary

[编辑] 缺陷报告

以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 534 C++98 std::basic_string 没有成员函数 front() 已添加

[编辑] 参见

(DR*)
访问最后一个字符
(公共成员函数) [编辑]
访问第一个字符
(std::basic_string_view<CharT,Traits> 的公共成员函数) [编辑]