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

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

返回到字符串中首字符的引用。

如果 empty()true,那么行为未定义。

(C++26 前)

如果 empty()true,那么:

  • 如果实现是硬化实现,那么就会发生契约违背。并且契约违背处理函数在“观察”求值语义下返回时行为未定义。
  • 如果实现不是硬化实现,那么行为未定义。
(C++26 起)

目录

[编辑]返回值

operator[](0)

[编辑]复杂度

常数。

[编辑]注解

在 libstdc++ 中,front()不能在 C++98 模式中使用。

[编辑]示例

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

输出:

exemplary Exemplary

[编辑]缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 534 C++98 std::string 没有成员函数 front() 已添加

[编辑]参阅

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